curl Cheat Sheet: Requests, Headers, JSON, Auth and Timing
Checked against curl 8.23.0 (manpage and changelog); examples run with curl 8.7.1 on macOS on 28 November 2026. Each section links to the official docs it was checked against.
This sheet is for developers who poke at APIs from the terminal and want the right flag the first time. Flags were checked against the curl 8.23.0 manpage (released 14 October 2026), and most rows were run against httpbin.org with the curl 8.7.1 that ships with macOS. Rows marked untested use a flag newer than that build.
When to use what
For a JSON API, use --json and stop hand-writing Content-Type headers. It sets both Content-Type and Accept to application/json and sends the body as-is. Use -d only for classic form posts, and remember it strips newlines when it reads from a file; --data-binary @file sends the bytes untouched.
Don't reach for -X unless the method has no flag of its own. -d already makes a POST, -T a PUT, -I a HEAD. When you do set -X and also follow redirects, prefer --follow (8.16.0+) over -L, because -L repeats your custom method on every hop.
In scripts, always add --fail (or --fail-with-body if you want to log the error text), -sS so errors still print, and a -m ceiling so a dead server can't hang a CI job. Add --retry for flaky upstreams; by default it only retries timeouts and 408, 429, 500, 502, 503, 504, 522 and 524, and it honours Retry-After.
Once a command works, paste it into our curl converter or curl to fetch tool to get the same request in code. Coming from Postman, Postman to curl goes the other way. The post on curl for API testing walks through a longer session.
curl picks the method from the body flag you use. -X only changes the word on the request line; it doesn't change how curl behaves, so -X HEAD still waits for a body.
curl --json '{"name":"ada"}' https://api.example.com/usersPOST a JSON body with Content-Type and Accept both set to application/json
curl --json @body.json URLSend JSON read from a file; use @- to read it from stdin
curl -d 'a=1&b=2' URLPOST a form body as application/x-www-form-urlencoded
curl --data-binary @payload.bin URLPOST a file byte for byte, keeping newlines that -d @file would strip
curl --data-raw '@not-a-file' URLSend data that starts with @ literally instead of reading a file
curl -G --data-urlencode 'q=a b&c' URLTurn the data into a URL-encoded query string on a GET request
Tested: became ?q=a+b%26c
curl --url-query 'page=2' URLAppend one encoded name=value pair to the URL query, even on a POST
curl -X DELETE URLSend a method that has no dedicated flag, such as DELETE or PATCH
curl -I URLMake a real HEAD request and print only the response headers
curl -H 'X-Request-Id: 42' URLAdd or replace a request header; repeat -H for more headers
curl -H 'User-Agent:' URLRemove a header curl would send by default by giving it an empty value
curl -i URLPrint response headers above the body (named --show-headers since 8.10.0)
curl -D headers.txt -o body.json URLSave response headers and body to separate files
curl -v URLShow the request line, TLS handshake and both header sets on stderr
curl -sS URLHide the progress meter but still print errors
curl -O https://example.com/file.tar.gzSave the download under the file name taken from the URL
curl --compressed URLAsk for a compressed response and decompress it on arrival
curl -L URLFollow Location redirects, up to 50 by default
curl -X POST --follow URLFollow redirects while letting 301, 302 and 303 switch a custom method to GET
curl -L --max-redirs 5 URLStop following after five redirects
curl --fail URLExit with code 22 and no body when the response is 400 or higher
curl --fail-with-body URLExit with code 22 on a 4xx or 5xx but still print the error body
Tested: 500 gave exit 22
curl --retry 3 URLRetry timeouts and 408, 429, 500, 502, 503, 504, 522, 524 with doubling waits
curl --retry 3 --retry-all-errors URLRetry on any error, including 4xx responses and refused connections
curl --retry 5 --retry-max-time 60 URLCap the total time spent retrying at 60 seconds
curl --connect-timeout 5 -m 30 URLAllow 5 seconds to connect and 30 seconds for the whole transfer
Tested: -m 2 on a 5 s delay exited with code 28
All time_ values are seconds from the start of the transfer, so each one includes the steps before it. Subtract neighbours to get the length of a single phase.
curl -s -o /dev/null -w '%{http_code}\n' URLPrint only the response status code, handy in shell tests
curl -s -o /dev/null -w '%{time_namelookup} %{time_connect} %{time_appconnect} %{time_starttransfer} %{time_total}\n' URLBreak a request into DNS, TCP connect, TLS, first byte and total time
curl -sL -o /dev/null -w '%{num_redirects} %{url_effective}\n' URLShow how many redirects were followed and the final URL
curl -s -o /dev/null -w '%{num_retries}\n' --retry 2 URLReport how many retries actually ran (variable added in 8.9.0)
curl -s -o /dev/null -w '%{json}' URLDump every write-out variable as one JSON object for logging
curl -s -o /dev/null -w '%{header_json}' URLPrint the response headers as a JSON object
curl --cacert ca.pem URLTrust a private CA bundle instead of switching verification off
curl --ca-native URLUse the operating system's certificate store (added in 8.2.0)
curl --cert client.pem --key client.key URLPresent a client certificate for mutual TLS
curl --tlsv1.2 --tls-max 1.2 URLPin the TLS version to 1.2 to debug a handshake problem
curl --resolve api.example.com:443:10.0.0.5 https://api.example.com/Send the request to a chosen IP while keeping the real hostname for SNI and Host
curl --connect-to api.example.com:443:staging.internal:443 URLReroute one host and port pair to another without touching DNS
curl -F '[email protected];type=application/pdf' URLUpload a file as multipart/form-data with an explicit MIME type
curl --form-string 'note=@literal' URLSend a form field whose value starts with @ or < without reading a file
curl -T file.json URLPUT a file as the raw request body
Tested: httpbin echoed method PUT
curl -k URLSkip certificate checks; only for a throwaway local test, never in scripts
Worked example: Find out why an API call is slow, then make the script safe
Start by timing a single request. Send the body to /dev/null so only the numbers print:
curl -s -o /dev/null \
-w 'dns %{time_namelookup}\nconnect %{time_connect}\ntls %{time_appconnect}\nttfb %{time_starttransfer}\ntotal %{time_total}\n' \
https://httpbin.org/get
One run from our machine printed:
dns 0.003310
connect 0.228426
tls 0.454987
ttfb 0.608384
total 0.608697
Read it as running totals. DNS took 3 ms (cached). The TCP connect took about 225 ms, and the TLS handshake another 227 ms, which is two round trips to a far-away server. The server then needed about 153 ms to produce the first byte, and the body arrived almost at once. So most of the time here is network distance, not the API itself; reusing one connection, or calling from a nearer region, would help more than tuning the endpoint.
Now wrap the call so it can live in a deploy script:
curl -sS --fail-with-body --connect-timeout 5 -m 30 \
--retry 3 --retry-max-time 60 \
--json '{"tool":"curl"}' https://httpbin.org/post
echo "exit=$?"
If the server answers 2xx, you get the JSON and exit 0. A 4xx or 5xx prints the error body and exits 22, so set -e stops the script. A hang ends with exit 28 after 30 seconds instead of blocking the job forever, and a 503 gets up to three retries before curl gives up.
Common mistakes
Using
-X POSTtogether with-d, or-X GETto make a HEAD request.Drop
-X:-dalready makes a POST. For headers only use-I, because-X HEADmakes curl wait for a body that never comes.Assuming a script succeeded because curl exited 0 on a 404 or 500.
Without
--fail, any HTTP response counts as success. Add--failor--fail-with-bodyso error statuses return exit code 22.Posting a pretty-printed JSON file with
-d @file.jsonand wondering why a signature or line-based parser breaks.-d @filestrips carriage returns and newlines. Use--json @file.jsonor--data-binary @file.jsonto send the bytes unchanged.Fixing a certificate error with
-kand leaving it in the script.Point curl at the right CA with
--cacertor--ca-native.-kturns off the check that stops a man-in-the-middle from reading your token.Following a redirect with
-Lafter-X POSTand getting an unexpected POST to the new URL.-Lreuses your custom method on every hop. Use--followon curl 8.16.0 or later, or drop-Xand let-dset the method.Putting a bearer token straight on the command line on a shared machine.
It lands in shell history and is visible in the process list. Read it from an env var, a
~/.netrcentry with-n, or a config file passed with-K.
Related cheat sheets
Sources
- https://curl.se/docs/manpage.html
- https://curl.se/changes.html
- https://curl.se/info
- https://httpbin.org/
How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.