How-to

Show, Send and Remove Headers with curl

Headers carry the metadata of every HTTP exchange: caching, compression, cookies, security policies. curl lets you read them all and write any you need.

How do I see the response headers?

FlagShowsNotes
-IHeaders only, via a HEAD requestSome apps answer HEAD differently from GET
-iHeaders, then the bodyReal GET
-D fileHeaders written to a file-D - writes them to the terminal
-vRequest and response headers plus TLS detailsOutput on stderr

To see headers of a real GET without the body, combine them: curl -sS -D - -o /dev/null https://shop.example.net/. This avoids being misled by servers that handle HEAD requests in a separate code path.

How do I send my own headers?

curl -H 'Accept-Language: es-ES' -H 'X-Request-Id: test-4410' https://shop.example.net/

Repeat -H for each header. To keep many headers in a file, write one per line and use -H @headers.txt, which also keeps tokens out of shell history. Replacing a header curl sends by default, such as Accept or User-Agent, works the same way; the agent string has its own page, Set a curl user agent.

How do I remove a header curl adds?

curl -H 'Accept:' https://api.example.net/      # drop the Accept header entirely
curl -H 'X-Empty;' https://api.example.net/      # send X-Empty with an empty value

A header name followed by a colon and nothing else removes it; a semicolon sends it empty. Useful for testing how an application behaves without headers it normally receives.

How do I read a single header value?

curl 7.84 and later can print one response header directly:

curl -sS -o /dev/null -w '%header{content-type}\n' https://shop.example.net/
curl -sS -o /dev/null -w '%{header_json}' https://shop.example.net/ | jq '."cache-control"'

The JSON form returns all headers as an object, ideal for scripts that check several values at once.

How do I test caching and compression?

curl -sS -D - -o /dev/null -H 'Accept-Encoding: br, gzip' https://shop.example.net/app.css | grep -iE 'content-encoding|cache-control|etag|age'
curl -sS -o /dev/null -w '%{size_download}\n' --compressed https://shop.example.net/app.css

The first line shows whether the server compresses and how long browsers and proxies may cache the file. A Content-Encoding: br or gzip line means compression works. Long Cache-Control: max-age values on static files reduce repeat traffic, which helps keep a busy site well inside the 15 TB outgoing allowance each machine includes.

To test conditional requests, send the ETag back: curl -sS -o /dev/null -w '%{http_code}\n' -H 'If-None-Match: "6750a1-4f2c"' https://shop.example.net/app.css. A 304 means the cache check works.

How do I test virtual hosts and new machines?

For plain HTTP, set the Host header to reach a specific site on a shared address:

curl -sS -D - -o /dev/null -H 'Host: blog.example.net' http://203.0.113.25/

For HTTPS, the TLS layer also needs the name, so use --resolve blog.example.net:443:203.0.113.25 and the real URL. That tests the certificate, the server block and the application exactly as visitors will see them after a DNS change.

Which security headers should I check on my sites?

  • Strict-Transport-Security: forces HTTPS in browsers.
  • Content-Security-Policy: limits where scripts and styles may load from.
  • X-Content-Type-Options: nosniff: stops content type guessing.
  • Referrer-Policy: controls what the Referer header reveals.
curl -sS -D - -o /dev/null https://shop.example.net/ | grep -iE 'strict-transport|content-security|x-content-type|referrer-policy'

Missing lines are easy to add in nginx with add_header ... always;. Redirect headers are covered in Follow redirects with curl.

How do I compare headers from two servers?

Fetch the same URL from the old and the new machine and diff the header blocks. diff <(curl -sS -D - -o /dev/null --resolve shop.example.net:443:198.51.100.12 https://shop.example.net/) <(curl -sS -D - -o /dev/null --resolve shop.example.net:443:203.0.113.25 https://shop.example.net/) highlights a missing security header, a different cache policy or a changed server banner in one view, which is exactly what you want to see before switching DNS during a migration.

Keep Reading

Set a curl user agent

Change or remove the User-Agent header curl sends, test how your own server treats different clients and read agents in your logs.

Open

The curl command

A practical tour of curl for server admins: fetching, saving, timing, debugging, retrying, resolving and the flags that make scripts reliable.

Open

Cookies with curl

Send cookies, save them to a jar, reuse sessions across requests and script a login flow with curl, plus how to read the jar format.

Open

Outbound traffic allowance

How the 15 TB monthly egress allowance works, what it means per day and per second, and why inbound data costs nothing.

Open

FAQ

Quick Answers

Use -I for a HEAD request, -i to include headers with the body, or -D - with -o /dev/null for a GET without the body.

Use -H 'Name: value', repeated for each header.

Pass -H with the header name and a colon but no value, such as -H 'Accept:'.

On curl 7.84 or later, use -w '%header{name}'.

Use --resolve with the host name, port and new IP address.