brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: curl -D saves the response headers next to the download

Curl
~/tips/tip-curl-dump-header-file

curl -sL -D headers.txt -o page.html https://example.com/

Keep the headers of a download in a separate file while the body goes to its own file.

-I gives you only the headers, and -i mixes them into the body. -D does a normal download and writes the headers to a file of their own.

Give -D a file name:

curl -sL -D headers.txt -o page.html https://example.com/
cat headers.txt

The page lands in page.html untouched, and headers.txt holds the status line and every header. With -L you get the headers of each hop in the redirect chain, one block after the other, which is a nice record of how you got there.

Use -D - to print the headers on standard output while the body still goes to a file. I like that for a quick look at caching headers on a real GET, because some servers answer a HEAD request from -I differently than a GET.

curl -s -D - -o /dev/null https://example.com/ | grep -i '^cache-control'

Header lines end with a carriage return and a newline, as HTTP sends them. That trips up scripts: a value you cut out with grep still carries the \r, and a comparison like [ "$x" = "no-cache" ] fails for no visible reason. Pipe through tr -d '\r' first. The file is overwritten on every run unless you choose another name.

Further reading: curl man page

More Curl tips curl --trace-ascii – https://example.com/ Retry-After: 120 curl -fsS --fail-early \ url = "https://example.com/data/latest.json" curl -4 -s -o /dev/null -w '%{remote_ip} %{http_code}\n' https://example.com/ All Curl tips (71) All topics

Link