curl Cheat Sheet: The Commands You Actually Use, Explained
curl is the HTTP client that is already installed on your machine, your CI runner, your Docker image, and the server you just SSH'd into. It is also the format every API's documentation secretly uses: when a doc shows you a request, it shows you a curl command. This cheat sheet covers the flags you will actually use, the ones that save you during debugging, and the shell traps that make curl look broken when it is not.
The anatomy: method, header, body
Three flags cover most requests: -X sets the method, -H adds a header, -d attaches a body.
curl -X POST -H "Content-Type: application/json" -d '{"name":"ada"}' https://api.example.com/users
Two things to know. First, -d implies POST, so -X POST is redundant there; you only need -X for PUT, PATCH, and DELETE. Second, -d does not set a Content-Type for you: it defaults to application/x-www-form-urlencoded, which is why APIs reject JSON bodies sent without the header.
GET requests with query strings
Never hand-assemble query strings. Use -G with --data-urlencode and curl encodes each parameter correctly:
curl -G https://api.example.com/search --data-urlencode "q=hello world" --data-urlencode "lang=en"
This produces ?q=hello%20world&lang=en. Spaces, ampersands, and non-ASCII text are handled for you. If you paste a raw query string into the URL instead, quote the whole URL or the shell will split it at the & and run half your request as a background job.
Sending JSON the short way
Since curl 7.82, --json bundles the three flags a JSON POST needs: it sets the Content-Type and Accept headers and attaches the body.
curl --json '{"name":"ada"}' https://api.example.com/users
To send a JSON file instead of an inline string, use --json @payload.json. The @ prefix reads from a file with -d too, but --json gets the headers right in one step.
Forms and file uploads
HTML-style form posts use -d with key=value pairs (repeat the flag for each field). Multipart uploads, the kind with files, use -F:
curl -F "[email protected]" -F "title=Q3 report" https://api.example.com/upload
-F builds a proper multipart/form-data request; -d @file sends the raw file bytes as the whole body, which is what you want for a plain PUT of a file to signed storage URLs. Mixing them up is a classic source of "the server says my file is empty."
Auth patterns
- Basic auth:
-u username:password(prompts for the password if you write-u username, keeping it out of your shell history) - Bearer tokens:
-H "Authorization: Bearer $TOKEN", with the token in an environment variable so it never lands in the command itself - API keys: usually just a header,
-H "X-API-Key: ..." - Cookies:
-c jar.txtsaves cookies from a response,-b jar.txtsends them on the next request; use both together to stay logged in across a scripted session
Seeing what actually happened
When a request misbehaves, three flags tell you everything. -i includes the response headers (status line, cookies, caching) above the body. -v shows the full conversation: the exact request curl sent, TLS details, and every response header. And -w prints timing and status after the body:
curl -s -o /dev/null -w "%{http_code} %{time_total}s\n" https://api.example.com/health
That last one is the uptime-check pattern: discard the body, print just the status code and total time. Pair -s (silent) with -S (show errors) as -sS in scripts so progress noise disappears but real failures still surface.
Downloads that behave
-o name.zip saves to a filename you choose; -O keeps the server's filename. -L follows redirects, and you almost always want it for downloads, since most file hosts 302 to a CDN. --fail (or -f) makes curl exit non-zero on HTTP errors instead of happily saving an HTML error page as your download. A resumable download adds -C -, which picks up where the previous attempt stopped.
The working download incantation: curl -fLO --retry 3 -C - https://example.com/big.tar.gz
The quoting trap
Most "curl is broken" reports are shell quoting. Single quotes protect everything but cannot contain a single quote; double quotes allow $VARIABLE expansion but also let the shell eat backticks and ! in interactive shells. A JSON body with a variable in it needs the hybrid: -d '{"name":"'"$NAME"'"}', or better, build the body with a tool like jq. On Windows, cmd.exe does not understand single quotes at all: use double quotes outside and escape the inner ones.
And a word on -k (--insecure): it disables certificate verification. Fine against your own staging box with a self-signed cert, never fine as a permanent fix. If you need it in production, the real problem is a missing or misconfigured certificate.
Quick reference
| Flag | Does |
|---|---|
-X PUT | Set the HTTP method |
-H "K: V" | Add a request header (repeatable) |
-d data | Request body; implies POST; @file reads a file |
--json data | JSON body with correct headers in one flag |
-G --data-urlencode | Build an encoded query string for GET |
-F field=@file | Multipart form upload |
-u user:pass | Basic auth |
-b / -c jar | Send / save cookies |
-i / -v | Show response headers / full debug trace |
-sS | Quiet but still show errors |
-o / -O | Save body to named / server-named file |
-L | Follow redirects |
--fail | Exit non-zero on HTTP errors |
-C - | Resume an interrupted download |
-w format | Print status, timing, sizes after the body |
--retry 3 | Retry transient failures |
Putting it together: an API test in one line
curl -sS --fail --json '{"email":"[email protected]"}' https://api.example.com/users | jq .id
Quiet unless something fails, non-zero exit when the API returns an error, JSON headers handled, and the response piped into jq for the one field you care about. This pattern drops straight into CI scripts and health checks.
When your query strings get complicated, our URL Encoder shows exactly how each character encodes, useful for checking what curl will send. Related reading: HTTP status codes for reading the responses, URL encoding for the percent-encoding rules, and DNS record types for when curl cannot resolve the host at all.
Try it yourself
Use the free browser-based URL Encoder on DevBench — no signup, runs entirely in your browser.
Open URL Encoder