curl Cheat Sheet
curl flags and patterns: methods, headers, auth, data, cookies, and common API calls.
curl transfers data using URLs and is especially useful for testing HTTP APIs from a terminal. Start with a simple request, then add only the flags needed for headers, data, authentication, or diagnostics.
Basic requests and flags
A URL alone performs a GET and writes the response body to standard output. Use --fail-with-body in scripts when HTTP errors should produce a non-zero exit status while retaining the response.
curl https://api.example.com/health
curl --fail-with-body --silent --show-error https://api.example.com/health
| Flag | Use |
|---|---|
-X, --request METHOD | Select an HTTP method explicitly. |
-H, --header NAME: VALUE | Add a request header. |
-d, --data DATA | Send request data; commonly changes the method to POST. |
-o, --output FILE | Write the response to a named file. |
-i, --include | Include response headers in output. |
-L, --location | Follow redirects. |
-s, --silent | Hide progress and errors. |
-S, --show-error | Show errors when silent mode is enabled. |
-v, --verbose | Show request and connection details. |
-u, --user USER:PASSWORD | Send Basic authentication credentials. |
-F, --form NAME=VALUE | Build a multipart form request. |
-w, --write-out FORMAT | Print selected transfer metadata. |
HTTP methods
Use explicit methods when the server contract requires them. A request body is usually supplied with --data, --json, or --form.
curl -X GET https://api.example.com/items
curl -X POST https://api.example.com/items
curl -X PUT https://api.example.com/items/42
curl -X PATCH https://api.example.com/items/42
curl -X DELETE https://api.example.com/items/42
| Method | Common intent |
|---|---|
GET | Retrieve a representation. |
POST | Create a resource or trigger an action. |
PUT | Replace a resource representation. |
PATCH | Apply a partial update. |
DELETE | Remove a resource. |
Headers and JSON
Headers describe the request and its payload. Modern curl versions support --json, which adds JSON content and accept headers; the expanded form is useful when compatibility matters.
curl https://api.example.com/items \
-H 'Accept: application/json'
curl https://api.example.com/items \
-X POST \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
--data '{"name":"Ada","active":true}'
curl --json '{"name":"Ada"}' https://api.example.com/items
curl --json @payload.json https://api.example.com/items
Downloads and forms
-O uses the remote filename; -o chooses one. Use -L when a download URL redirects to a final location.
curl -L -O https://downloads.example.com/archive.tar.gz
curl -L -o latest.tar.gz https://downloads.example.com/archive.tar.gz
curl -F 'file=@report.pdf' -F 'description=Quarterly report' https://api.example.com/upload
Inspect responses
--write-out is useful for scripts and smoke tests because it can emit a stable status code or timing value after the body.
curl -sS -o /dev/null -w 'status=%{http_code}\ntime=%{time_total}s\n' \
https://api.example.com/health
curl -i -v https://api.example.com/health