Curl Send Header Example

-H adds a header line to the request. The same flag also replaces curl's own headers and removes them, depending on what you put after the colon.

Last updated . How this page is checked.

The command

curl https://httpbin.org/headers \
  -H "X-Request-Id: 7f3a9c" \
  -H "Accept-Language: en-GB"

Opens the request console with this request filled in. It is sent from api-tester.com's server rather than from your machine, so it shows what the endpoint returns, not how your own network or curl build behaves.

What the flags do

FlagWhat it does
-H "Name: value"Add a header, or replace one curl would have sent.
-H "Name:"Remove a header curl sends by default.
-H "Name;"Send the header with an empty value.
-H @fileRead headers from a file, one per line (curl 7.55+).
-vShow the request headers actually sent, on lines starting with >.

Add, replace, remove

Curl sends Host, User-Agent and Accept: */* on its own, and more once you add a body. Naming one of those in -H replaces it. Naming it with nothing after the colon removes it, which is how you test what an API does when a header is absent. A header that should be present but empty needs a semicolon instead of a colon, because "X-Flag:" already means "remove".

curl https://httpbin.org/headers \
  -H "Accept: application/json" \
  -H "User-Agent:" \
  -H "X-Empty;"

Checking what was sent

Do not assume; look. -v prints the outgoing request with each line prefixed >. An echo endpoint such as httpbin.org/headers shows the headers as the server received them, which also reveals anything a proxy added or removed on the way.

For a set of headers you reuse, put them in a file and pass -H @headers.txt, which keeps tokens out of your shell history.

curl -v -o /dev/null -H "X-Debug: 1" https://httpbin.org/get 2>&1 | grep "^>"

Common mistakes

  • Custom headers follow redirects. With -L, headers set by -H are sent to every host in the chain; think before attaching a secret to a URL that might redirect elsewhere.
  • Do not set Content-Length or Host by hand unless that is the thing under test. Curl computes them, and a wrong value produces hangs or 400s.
  • Quote the whole header. Unquoted, the space after the colon splits it into two arguments and curl treats the value as a URL.

Related curl examples

More under Headers

References

These links leave api-tester.com, which is independent of the projects they point to.

FAQ

How do I send a header with curl?

Use -H "Name: value". Repeat the flag for each additional header.

How do I send multiple headers with curl?

Pass one -H flag per header, for example curl -H "Accept: application/json" -H "X-Api-Version: 2" URL.

How do I remove a default header in curl?

Pass the header name followed by a colon and nothing else, for example -H "Accept:" to stop curl sending its Accept header.