Skip to content

Menu

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/users

POST a JSON body with Content-Type and Accept both set to application/json

curl --json @body.json URL

Send JSON read from a file; use @- to read it from stdin

From the docs, not run by us
curl -d 'a=1&b=2' URL

POST a form body as application/x-www-form-urlencoded

curl --data-binary @payload.bin URL

POST a file byte for byte, keeping newlines that -d @file would strip

From the docs, not run by us
curl --data-raw '@not-a-file' URL

Send data that starts with @ literally instead of reading a file

From the docs, not run by us
curl -G --data-urlencode 'q=a b&c' URL

Turn the data into a URL-encoded query string on a GET request

Tested: became ?q=a+b%26c

curl --url-query 'page=2' URL

Append one encoded name=value pair to the URL query, even on a POST

From the docs, not run by us
curl -X DELETE URL

Send a method that has no dedicated flag, such as DELETE or PATCH

curl -I URL

Make a real HEAD request and print only the response headers

curl -H 'X-Request-Id: 42' URL

Add or replace a request header; repeat -H for more headers

Try it: http-header-generatorFrom the docs, not run by us
curl -H 'User-Agent:' URL

Remove a header curl would send by default by giving it an empty value

From the docs, not run by us
curl -i URL

Print response headers above the body (named --show-headers since 8.10.0)

Try it: HTTP Header ParserFrom the docs, not run by us
curl -D headers.txt -o body.json URL

Save response headers and body to separate files

curl -v URL

Show the request line, TLS handshake and both header sets on stderr

From the docs, not run by us
curl -sS URL

Hide the progress meter but still print errors

curl -O https://example.com/file.tar.gz

Save the download under the file name taken from the URL

From the docs, not run by us
curl --compressed URL

Ask for a compressed response and decompress it on arrival

From the docs, not run by us

When curl follows a redirect to another host, it drops credentials set with -u, --oauth2-bearer or a Cookie header. Only --location-trusted sends them on, so use it only for hosts you control.

curl -u alice:secret URL

Send HTTP Basic auth; leave off :secret and curl prompts for the password

curl --oauth2-bearer "$TOKEN" URL

Send an Authorization: Bearer header from a variable

curl -H "Authorization: Bearer $TOKEN" URL

Set the bearer header by hand, the form most API docs show

From the docs, not run by us
curl -c jar.txt URL

Write cookies the server sets into a Netscape-format cookie jar file

curl -b jar.txt URL

Send the cookies stored in a jar file with this request

curl -b 'session=xyz' URL

Send a cookie given inline as name=value

From the docs, not run by us
curl -n URL

Read the login for this host from ~/.netrc so it stays out of shell history

From the docs, not run by us
curl -L URL

Follow Location redirects, up to 50 by default

curl -X POST --follow URL

Follow redirects while letting 301, 302 and 303 switch a custom method to GET

From the docs, not run by us
curl -L --max-redirs 5 URL

Stop following after five redirects

From the docs, not run by us
curl --fail URL

Exit with code 22 and no body when the response is 400 or higher

curl --fail-with-body URL

Exit with code 22 on a 4xx or 5xx but still print the error body

Tested: 500 gave exit 22

curl --retry 3 URL

Retry timeouts and 408, 429, 500, 502, 503, 504, 522, 524 with doubling waits

curl --retry 3 --retry-all-errors URL

Retry on any error, including 4xx responses and refused connections

From the docs, not run by us
curl --retry 5 --retry-max-time 60 URL

Cap the total time spent retrying at 60 seconds

From the docs, not run by us
curl --connect-timeout 5 -m 30 URL

Allow 5 seconds to connect and 30 seconds for the whole transfer

Tested: -m 2 on a 5 s delay exited with code 28

From the docs, not run by us

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' URL

Print 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' URL

Break a request into DNS, TCP connect, TLS, first byte and total time

curl -sL -o /dev/null -w '%{num_redirects} %{url_effective}\n' URL

Show how many redirects were followed and the final URL

curl -s -o /dev/null -w '%{num_retries}\n' --retry 2 URL

Report how many retries actually ran (variable added in 8.9.0)

From the docs, not run by us
curl -s -o /dev/null -w '%{json}' URL

Dump every write-out variable as one JSON object for logging

From the docs, not run by us
curl -s -o /dev/null -w '%{header_json}' URL

Print the response headers as a JSON object

From the docs, not run by us
curl --cacert ca.pem URL

Trust a private CA bundle instead of switching verification off

Try it: SSL Cert DecoderFrom the docs, not run by us
curl --ca-native URL

Use the operating system's certificate store (added in 8.2.0)

From the docs, not run by us
curl --cert client.pem --key client.key URL

Present a client certificate for mutual TLS

From the docs, not run by us
curl --tlsv1.2 --tls-max 1.2 URL

Pin the TLS version to 1.2 to debug a handshake problem

Try it: SSL Certificate CheckerFrom the docs, not run by us
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

From the docs, not run by us
curl --connect-to api.example.com:443:staging.internal:443 URL

Reroute one host and port pair to another without touching DNS

From the docs, not run by us
curl -F '[email protected];type=application/pdf' URL

Upload a file as multipart/form-data with an explicit MIME type

curl --form-string 'note=@literal' URL

Send a form field whose value starts with @ or < without reading a file

From the docs, not run by us
curl -T file.json URL

PUT a file as the raw request body

Tested: httpbin echoed method PUT

curl -k URL

Skip certificate checks; only for a throwaway local test, never in scripts

From the docs, not run by us

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 POST together with -d, or -X GET to make a HEAD request.

    Drop -X: -d already makes a POST. For headers only use -I, because -X HEAD makes 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 --fail or --fail-with-body so error statuses return exit code 22.

  • Posting a pretty-printed JSON file with -d @file.json and wondering why a signature or line-based parser breaks.

    -d @file strips carriage returns and newlines. Use --json @file.json or --data-binary @file.json to send the bytes unchanged.

  • Fixing a certificate error with -k and leaving it in the script.

    Point curl at the right CA with --cacert or --ca-native. -k turns off the check that stops a man-in-the-middle from reading your token.

  • Following a redirect with -L after -X POST and getting an unexpected POST to the new URL.

    -L reuses your custom method on every hop. Use --follow on curl 8.16.0 or later, or drop -X and let -d set 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 ~/.netrc entry with -n, or a config file passed with -K.

Sources

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.

What changed in AI dev tools, once a month

Renames, shutdowns, pricing changes and new MCP setups, each with a source.

Subscribe
Reviews and commentsShow

Comments

Sign in to leave a comment.

Loading comments…