How-to

Send POST Requests with curl

curl switches to POST as soon as you give it data. The flag you pick decides how that data is encoded and which Content-Type the server sees.

What is the simplest POST?

curl -d 'email=ana@example.net&plan=s1' https://forms.example.net/subscribe

-d (or --data) sends the body with Content-Type: application/x-www-form-urlencoded, exactly like an HTML form. The method becomes POST automatically; adding -X POST is unnecessary, and with redirects it can even cause wrong behavior, as Follow redirects with curl explains.

Which data flag should I use?

FlagEncodingUse for
-dSent as given, newlines stripped from filesSimple form fields
--data-urlencodePercent-encodes the valueFields with spaces or symbols
--data-binarySent byte for byteFiles where newlines matter
--data-rawLike -d, but @ is not specialValues starting with @
--jsonRaw, with JSON headersAPIs
-Fmultipart/form-dataFile uploads and mixed forms
curl --data-urlencode 'note=Back in 5 min & ready' --data-urlencode 'ticket=2207' https://desk.example.net/notes

How do I send JSON?

curl 7.82 and later have --json, which sends the body unchanged and sets both Content-Type: application/json and Accept: application/json:

curl --json '{"name":"backup-ams","size_gb":250}' https://api.example.net/v1/volumes
curl --json @payload.json https://api.example.net/v1/jobs

Every current image has a new enough curl. On older systems, write the headers yourself: -H 'Content-Type: application/json' --data-binary @payload.json. Build JSON with jq -n in scripts rather than gluing strings, so quotes in values never break the body.

How do I upload a file?

curl -F 'report=@/var/reports/2026-11.pdf;type=application/pdf' -F 'month=2026-11' https://files.example.net/upload

-F builds a multipart form, the encoding browsers use for file inputs. @ attaches a file; ;type= sets its content type; ;filename= changes the name the server sees. To send a file's contents as a plain field value instead, use < in place of @.

For APIs that expect the raw file as the body rather than a form, use --data-binary @file with an explicit Content-Type, or -T file for a PUT upload.

How do I authenticate?

curl -u deploy:"$API_PASS" --json @release.json https://ci.example.net/api/releases
curl -H "Authorization: Bearer $TOKEN" --json '{"ok":true}' https://api.example.net/v1/ping

Keep secrets in environment variables or a file read with -H @headers.txt, so they do not appear in shell history or in the process list that other users can see with ps.

How do I check the result in a script?

By default curl exits 0 even on an HTTP 500. Make it fail on errors and capture the status:

code=$(curl -sS -o /tmp/resp.json -w '%{http_code}' --json @job.json https://api.example.net/v1/jobs)
[ "$code" = 201 ] || { echo "API returned $code"; cat /tmp/resp.json; exit 1; }

--fail-with-body (curl 7.76 and later) is a shorter alternative: it returns a non-zero exit code on HTTP errors and still saves the error body for debugging. Add --retry 3 --retry-all-errors for calls that may hit a brief outage.

How do I see exactly what was sent?

-v prints request headers; --trace-ascii trace.txt records the full exchange, body included. For checking request headers specifically, see Headers with curl. A quick local echo server for experiments is nc -l 9000 in one terminal and curl -d 'a=1' http://127.0.0.1:9000/ in another.

How do I send a large body without loading it into memory?

Use -T or --data-binary @file; curl streams the file from disk as it sends, so a multi-gigabyte upload does not fill RAM. Add -H 'Expect:' if the server stalls on the Expect: 100-continue header curl sends with large bodies; some older proxies wait for it and add a one-second delay to every upload.

How do I repeat a request a browser made?

Open the browser's developer tools, find the request in the network tab and use "Copy as cURL". The copied command includes every header and the body, ready to paste on your server. Remove cookies and tokens you do not need before saving it in a script, and replace secrets with environment variables, so the command can live in version control without exposing credentials.

Keep Reading

Headers with curl

Show response headers, send or remove request headers, pick single header values and test caching, compression and virtual hosts with curl.

Open

The curl command

A practical tour of curl for server admins: fetching, saving, timing, debugging, retrying, resolving and the flags that make scripts reliable.

Open

Follow redirects with curl

Make curl follow 301, 302, 307 and 308 redirects with -L, limit hops, keep POST bodies, see the final URL and debug redirect loops.

Open

Cookies with curl

Send cookies, save them to a jar, reuse sessions across requests and script a login flow with curl, plus how to read the jar format.

Open

FAQ

Quick Answers

Add data with -d, --json or -F; curl switches to POST automatically.

Use --json with a string or @file on curl 7.82 or later; it sets the JSON headers for you.

Use -F 'field=@/path/file' for a multipart form upload.

No. Data flags already make the request a POST, and -X can misbehave with redirects.

Use --fail or --fail-with-body, or check %{http_code} with -w.