Curl POST Example

Curl switches to POST the moment you give it a body. Which flag carries that body decides the Content-Type and the encoding, and picking the wrong one is behind most "the server says my POST is empty" reports.

Last updated . How this page is checked.

The command

curl -d "name=Ada&role=admin" https://httpbin.org/post

Opens the request console with this request filled in. It is sent from api-tester.com's server rather than from your machine, so it shows what the endpoint returns, not how your own network or curl build behaves.

What the flags do

FlagWhat it does
-d dataBody sent as-is with Content-Type: application/x-www-form-urlencoded.
--json dataBody plus JSON Content-Type and Accept headers (curl 7.82+).
-F name=valueA multipart/form-data body, the kind file uploads use.
--data-binary @fileA file sent byte for byte, newlines included.
-X POSTForces the method name. Only needed for a POST with no body at all.

Four ways to send a POST body

The request body and its Content-Type have to agree, because the server chooses a parser from the header. Curl sets a matching header for three of these four forms; with --data-binary it still claims form encoding unless you say otherwise.

# 1. form fields (the default)
curl -d "name=Ada&role=admin" https://httpbin.org/post

# 2. JSON
curl --json '{"name":"Ada","role":"admin"}' https://httpbin.org/post

# 3. multipart, as an HTML form with a file input would send
curl -F "name=Ada" -F "avatar=@photo.png" https://httpbin.org/post

# 4. a raw file, with the type stated explicitly
curl --data-binary @report.csv -H "Content-Type: text/csv" https://httpbin.org/post

A POST without a body

Some endpoints want a POST that carries nothing: triggering a job, rotating a key. Here -X POST is the right tool, but add an explicit empty body so curl sends Content-Length: 0. Several servers and proxies reject a bodiless POST that lacks it with 411 Length Required.

curl -X POST -d "" https://httpbin.org/post

Common mistakes

  • JSON sent with plain -d is labelled as a form. Add -H "Content-Type: application/json" or use --json, or expect 415 or an empty parsed body.
  • -d @file strips newlines. Use --data-binary for anything where line breaks matter.
  • After a 301 or 302, curl -L repeats the request as a GET, dropping the body. That is standard browser behaviour, not a bug; see the redirect example.

Related curl examples

More under Methods

References

  • curl man page: -d — How -d builds a request body and switches the method to POST.
  • MDN: POST — The method's semantics, including when it is and is not safe to repeat.

These links leave api-tester.com, which is independent of the projects they point to.

FAQ

How do I make a POST request with curl?

Pass a body with -d, for example curl -d "name=Ada" https://httpbin.org/post. Supplying data makes curl use POST automatically.

Do I need -X POST with curl?

No, not when you send data with -d, --json or -F. Use -X POST only for a POST that has no body.

What Content-Type does curl use for POST?

application/x-www-form-urlencoded for -d and --data-binary, application/json for --json, and multipart/form-data with a generated boundary for -F.