How can I set a custom HTTP header using cURL

David Y.

The Problem

How can I set a custom HTTP header on a request sent using cURL?

The Solution

We can do this using cURL’s -H/--header flag, which allows us to specify an HTTP header to be included in the request we’re sending.

curl --header "X-MyCustomHeader: HeaderValue"

If we need to add multiple custom headers to the request, we can do this with multiple --header flags:

curl --header "X-MyCustomHeader: HeaderValue" --header "X-AnotherHeader: Value"

To confirm that our request looks as we expect it to, we can have cURL print it out by providing the -v/--verbose flag as well:

curl --header "X-MyCustomHeader: HeaderValue" -v

The above command will show an HTTP request resembling the following:

> GET / HTTP/1.1 > Host: > User-Agent: curl/8.1.2 > Accept: */* > X-MyCustomHeader: HeaderValue

More information about cURL can be found by perusing its manual page, which is accessible on the cURL website or through the command man curl.

Join the discussionCome work with us
Share on Twitter
Bookmark this page
Ask a questionImprove this Answer

Related Answers

A better experience for your users. An easier life for your developers.

© 2023 • Sentry is a registered Trademark
of Functional Software, Inc.