The cheat sheet
| You want to… | Option |
|---|---|
| Send JSON | --json '{"name":"Aung"}' (curl 7.82 or newer) |
| Send a form | -d 'name=Aung&city=Bangkok' |
| Upload a file | -F 'file=@photo.jpg' |
| Use a method other than GET or POST | -X PUT, -X PATCH, -X DELETE |
| Add a header | -H 'Authorization: Bearer TOKEN' |
| Log in with a username and password | -u user:password |
| Build a query string safely | -G --data-urlencode 'q=thai food' |
| Follow redirects | -L |
| Fail on 4xx and 5xx | --fail or --fail-with-body |
| See response headers | -i (headers and body) or -v (everything) |
| Save the response | -o result.json |
| Print the status code and timing | -w '%{http_code} %{time_total}s\n' |
Add -s (silent) to hide the progress meter when you pipe the output somewhere.
Sending JSON: --json, not just -d
This is the most common mistake. -d on its own sends your JSON, but labels it as a form:
$ curl -s -d '{"name":"Aung"}' http://localhost:8899/users
→ Content-Type: application/x-www-form-urlencoded
Many APIs then reject the request or see an empty body. --json sets both headers for you:
$ curl -s --json '{"name":"Aung"}' http://localhost:8899/users
→ POST, Content-Type: application/json, Accept: application/json
On curl older than 7.82, write the header yourself:
-H 'Content-Type: application/json' -d '{"name":"Aung"}'. To send a JSON file, use --json @body.json
or -d @body.json.
Methods: when you need -X
cURL picks the method from what you send: no body means GET, and -d, --json or -F means POST.
Use -X only for other methods:
curl -X PUT -H 'Content-Type: application/json' -d '{"role":"editor"}' https://api.example.com/users/42
curl -X DELETE https://api.example.com/users/42
Avoid -X POST together with -d: it's redundant, and with -L it forces POST on redirects where
a GET was expected.
Forms and file uploads
-d sends a URL-encoded form, like a simple HTML form. -F sends multipart/form-data, which is
what file uploads need. The @ means “read this file”:
curl -F 'file=@notes.txt' -F 'title=Notes' https://api.example.com/upload → Content-Type: multipart/form-data; boundary=…
Query strings without encoding bugs
Spaces and symbols in a URL have to be encoded. Let cURL do it: -G turns -d and
--data-urlencode values into a query string on a GET request.
$ curl -s -G --data-urlencode 'q=thai food' -d page=2 http://localhost:8899/search → GET /search?q=thai+food&page=2
Authentication
curl -H 'Authorization: Bearer eyJhbGciOi…' https://api.example.com/me # token curl -u aung:secret https://api.example.com/me # basic auth
-u sends Authorization: Basic YXVuZzpzZWNyZXQ=, which is just aung:secret in Base64
(try it in the Base64 decoder). Anyone who sees the header can read the password, so only
use it over HTTPS. Commands with passwords or tokens also end up in your shell history; read them from a file or an environment
variable instead, for example -H "Authorization: Bearer $API_TOKEN".
Redirects and errors
By default cURL doesn't follow redirects, and it treats a 404 or 500 as success:
$ curl -s -o /dev/null -w '%{http_code}\n' http://localhost:8899/redirect
302
$ curl -s http://localhost:8899/missing >/dev/null; echo "exit $?"
exit 0
Add -L to follow redirects. In scripts, add --fail so an HTTP error makes cURL exit with code 22 and your
script stops. --fail-with-body does the same but still prints the error response, which usually explains what went wrong:
$ curl -s --fail-with-body http://localhost:8899/missing; echo " exit $?"
{"error":"not found"} exit 22
Seeing what happened
-iprints the response headers above the body.-vshows the request cURL sent, the TLS handshake and the response headers. It's the first thing to try when a request behaves differently in cURL and in your code.-wprints details after the transfer, such as%{http_code},%{time_total}or%{size_download}.-Isends a HEAD request for headers only. Some servers don't support HEAD and answer 501, so-iis often more reliable.
From cURL to code
Once a request works in cURL, you usually need it in your app. Browser DevTools can give you any request as a cURL command
(Network tab, right-click, Copy as cURL), and the cURL converter turns that into JavaScript
fetch, Axios or Python requests, keeping the headers, body and auth.
How this guide was checked: every example on this page was run as written, using curl 8.5.0 on Linux, against a local echo server that reports back what it received. Spotted a mistake? Tell us and we'll fix it.
Updated