The command
curl -d "name=Ada" -d "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
| Flag | What it does |
|---|---|
-d, --data | Send data. @file reads a file, stripping CR and LF. Sets POST and a form Content-Type. |
--data-raw | Same as -d, but a leading @ is sent literally instead of opening a file. |
--data-binary | Same as -d, but file content is sent byte for byte. |
--data-urlencode | Percent-encodes the value before sending it. |
The three side effects
Giving curl -d changes the method to POST, adds Content-Type: application/x-www-form-urlencoded, and adds a Content-Length. None of that depends on what the data looks like: JSON passed to -d is still announced as a form unless you override the header. Several -d flags are concatenated with & between them, which is handy for forms and wrong for anything else.
To send the same data in the URL of a GET instead, add -G.
curl -d '{"a":1}' -H "Content-Type: application/json" https://httpbin.org/post
curl -G -d "q=curl" -d "page=2" https://httpbin.org/get
Reading from files and stdin
-d @path reads the body from a file and -d @- reads it from standard input. In both cases -d removes carriage returns and newlines. That was designed for form data kept one field per line and it surprises everyone else. --data-binary takes the same @ syntax and changes nothing.
--data-urlencode has its own forms: name=value encodes the value, name@file encodes a file's content as the value, and a bare =value encodes without a name.
curl --data-binary @payload.json -H "Content-Type: application/json" https://httpbin.org/post
echo '{"a":1}' | curl --data-binary @- -H "Content-Type: application/json" https://httpbin.org/post
curl --data-urlencode "comment@message.txt" https://httpbin.org/post
curl --data-raw "@mention goes here" https://httpbin.org/post
Common mistakes
-dnever URL-encodes. A+in a value reaches the server as a space and a stray&starts a new field.- A value beginning with
@, such as a social handle or an email local part, makes-dlook for a file. Use--data-raw. - Large bodies trigger
Expect: 100-continueon HTTP/1.1, which adds a pause with servers that ignore it.-H "Expect:"removes it.
Related curl examples
Curl POST Example
Curl switches to POST the moment you give it a body.
Curl POST Form Example
HTML forms post in one of two encodings, and curl has a flag for each.
Curl POST JSON Example
Sending JSON takes two things: the JSON itself and a header saying that is what it is.
Curl Flags Example
Curl has well over two hundred options.
All Curl Examples
49 worked examples, grouped by what you are trying to do.
More under Request bodies
References
- curl man page: -d — The full description of --data and its file syntax.
- curl man page: --data-urlencode — The variant that percent-encodes values.
- curl man page: --data-binary — The variant that leaves newlines alone.
These links leave api-tester.com, which is independent of the projects they point to.
FAQ
What does the -d flag do in curl?
It sends the given data as the request body, makes the request a POST, and sets Content-Type to application/x-www-form-urlencoded.
What is the difference between --data and --data-binary?
When reading from a file, --data strips carriage returns and newlines, while --data-binary sends the file exactly as it is.
What is --data-raw in curl?
It behaves like -d but does not treat a leading @ as a filename, so the text is sent literally.