To POST JSON with curl, run curl --json '{"name":"widget","qty":3}' https://api.example.com/items, which sets the Content-Type and Accept headers to application/json for you. On curl older than 7.82.0, use curl -H "Content-Type: application/json" -d '{"name":"widget","qty":3}' https://api.example.com/items instead.
Every output on this page is real, captured from curl 8.8.0 against a small Python echo server on 127.0.0.1:8765 that replies with the method, the headers and the body it received. The URL http://127.0.0.1:8765/api/items stands in for your API.
The --json shortcut (curl 7.82.0 and later)
curl --json '{"name":"widget","qty":3}' http://127.0.0.1:8765/api/items
Example output (what the server received):
{"method": "POST", "content_type": "application/json", "accept": "application/json", "body": "{\"name\":\"widget\",\"qty\":3}", "valid_json": true}
The curl manual describes --json as a shortcut for three options at once: --data-binary, --header "Content-Type: application/json" and --header "Accept: application/json". It also makes the request a POST, so you don't need -X POST. Check your version with curl --version; the option was added in 7.82.0.
The classic way: -d plus a Content-Type header
curl -H "Content-Type: application/json" -d '{"name":"widget","qty":3}' http://127.0.0.1:8765/api/items
This works in every curl version. The header matters. Leave it out and curl labels the body as a form submission:
curl -d '{"name":"widget","qty":3}' http://127.0.0.1:8765/api/items
{"method": "POST", "content_type": "application/x-www-form-urlencoded", "accept": "*/*", ...}
The body bytes are identical, but a server framework that chooses its body parser from the Content-Type header will not treat this as JSON. Depending on the framework, your handler sees no parsed body, a validation error about missing fields, or the server rejects the request outright. If an API "ignores" your JSON, check this header first.
Option comparison
| Option | Content-Type sent | Accept sent | Newlines in an @file |
|---|---|---|---|
--json | application/json | application/json | Kept |
-d / --data | application/x-www-form-urlencoded unless you set -H | */* | Stripped |
--data-binary | application/x-www-form-urlencoded unless you set -H | */* | Kept |
--data-raw | Same as -d | */* | No file reading: @ is sent literally |
POST JSON from a file
For anything longer than one line, keep the JSON in a file and reference it with @:
curl --json @item.json http://127.0.0.1:8765/api/items
With item.json containing a pretty-printed object, the server received the file exactly, newlines included:
"body": "{\n \"name\": \"widget\",\n \"qty\": 3\n}\n"
The same file sent with -d @item.json arrived as { \"name\": \"widget\", \"qty\": 3}. The manual explains why: when -d reads from a file, carriage returns, newlines and null bytes are stripped out. That is harmless for JSON whitespace, but it corrupts a string value that contains a literal newline, and it breaks any body that isn't JSON. Use --json @file or --data-binary @file to send a file byte for byte.
To read the body from standard input, use @-. This is handy when another command produces the JSON:
echo '{"from":"stdin"}' | curl --json @- http://127.0.0.1:8765/api/items
PUT and PATCH with a JSON body
Add -X to change the method; the body options stay the same:
curl -X PUT --json '{"qty":4}' http://127.0.0.1:8765/api/items
curl -X PATCH -H "Content-Type: application/json" -d '{"qty":4}' http://127.0.0.1:8765/api/items
Example output for the PUT: {"method": "PUT", "content_type": "application/json", ...}.
curl POST JSON on Windows: the quoting problem
The single-quoted examples above are for Bash, zsh and Git Bash. On Windows they fail in a confusing way, because the double quotes inside the JSON never reach curl. These are the bodies the echo server received from curl.exe 8.13.0 on Windows 11:
| Shell | What you typed | Body the server received |
|---|---|---|
| Windows PowerShell 5.1 | curl.exe --json '{"name":"widget","qty":3}' URL | {name:widget,qty:3} |
| cmd.exe | curl --json '{"name":"widget","qty":3}' URL | '{name:widget,qty:3}' |
The server answered both with 400 Bad Request. Python's parser reported Expecting property name enclosed in double quotes: line 1 column 2 (char 1) for the first and Expecting value: line 1 column 1 (char 0) for the second; other servers word it differently, but it is always a JSON parse error.
cmd.exe doesn't treat single quotes as quotes at all. Wrap the JSON in double quotes and escape the inner ones with backslashes:
curl --json "{\"name\":\"widget\",\"qty\":3}" http://127.0.0.1:8765/api/items
Windows PowerShell 5.1 strips the inner double quotes when it builds the command line for a native program. Escape them with backslashes inside the single quotes:
curl.exe --json '{\"name\":\"widget\",\"qty\":3}' http://127.0.0.1:8765/api/items
Both of those arrived as valid JSON. Microsoft's about_Parsing documentation says PowerShell 7.3 changed how arguments containing quotes are passed to native commands and calls it a breaking change from 5.1; on 7.3 and later the plain single-quoted form keeps its quotes. The simplest fix on any Windows shell is to put the JSON in a file and use --json @item.json, which avoids quoting entirely.
Two more Windows details. In Windows PowerShell 5.1, curl without .exe is an alias for Invoke-WebRequest, which doesn't understand curl's options; type curl.exe. And curl.exe has been part of Windows since Windows 10 build 17063, but older builds shipped curl versions that predate --json, so run curl.exe --version first.
The PowerShell-native alternative
If you're in PowerShell anyway, Invoke-RestMethod avoids the quoting problem and parses the JSON response into objects:
$body = @{ name = 'widget'; qty = 3 } | ConvertTo-Json -Compress
Invoke-RestMethod -Uri http://127.0.0.1:8765/api/items -Method Post -ContentType 'application/json' -Body $body
The echo server received {"name":"widget","qty":3}. Microsoft's reference notes that from PowerShell 7.4 request bodies default to UTF-8 instead of ASCII. On 5.1 this matters: sending {"name":"café"} with -ContentType 'application/json' arrived with the é mangled, while -ContentType 'application/json; charset=utf-8' arrived intact.
See the response status and headers
By default curl prints only the response body. Add -i to include the status line and headers, which is the quickest way to see why a request failed:
curl -i --json '{"name":"widget"' http://127.0.0.1:8765/api/items
Example output (the JSON is missing its closing brace on purpose):
HTTP/1.0 400 Bad Request
Server: BaseHTTP/0.6 Python/3.12.2
Content-Type: application/json
Content-Length: 201
{"method": "POST", ..., "valid_json": false, "error": "Expecting ',' delimiter: line 1 column 17 (char 16)"}
Use -v to see the request side too. Lines starting with > are what curl sent:
> POST /api/items HTTP/1.1
> Host: 127.0.0.1:8765
> User-Agent: curl/8.8.0
> Content-Type: application/json
> Accept: application/json
> Content-Length: 7
If the Content-Type line is missing or wrong, that is your bug. The HTTP status codes guide explains what each error code means.
Make curl fail on HTTP errors in scripts
curl exits with code 0 even when the server returns 400 or 500, because the HTTP exchange itself worked. In scripts, add a fail option:
curl -s --fail-with-body --json '{"name":"widget"' http://127.0.0.1:8765/api/items; echo "exit=$?"
Example output: the error body, then exit=22. --fail also exits with 22 but discards the body, which hides the server's error message; --fail-with-body (curl 7.76.0 and later) keeps it. A server that isn't running gives exit code 7 instead, with curl: (7) Failed to connect to 127.0.0.1 port 8799 after 2039 ms: Couldn't connect to server when you add -sS.
Common mistakes
- Using shell variables inside single quotes.
'{"name":"$NAME"}'sends the literal text$NAME. Build the JSON with a real serializer and pipe it intocurl --json @-, which also escapes quotes inside the value. For example,python3 -c 'import json,sys; print(json.dumps({"name": sys.argv[1]}))' "$NAME" | curl --json @- URLturned the valuesay "hi"into valid JSON in our test. - Trailing commas and single-quoted keys. curl does not validate JSON; the manual says so explicitly.
{"a":1,}goes out as-is and the server rejects it. - Using
-F.-Fsendsmultipart/form-data, which is for file uploads, not JSON bodies. - Secrets on the command line. Tokens typed into
-H "Authorization: Bearer ..."land in shell history. Read them from an environment variable.
The same request from code: Python requests and JavaScript fetch.
Frequently asked questions
- Do I need -X POST when sending JSON with curl?
- No. Both -d and --json already make curl send a POST request, so -X POST is redundant. You only need -X when you want a different method with a body, for example -X PUT or -X PATCH together with --json.
- Which curl version added --json?
- The --json option was added in curl 7.82.0. Run curl --version to check. If your curl is older, use -H "Content-Type: application/json" with --data-binary or -d instead, which works in every version.
- Does curl check that my JSON is valid?
- No. The curl manual states that --json does not verify that the data is actual JSON or that its syntax is correct. curl sends whatever bytes you give it, and the server is the one that rejects malformed JSON, usually with a 400 Bad Request response.
- How do I send JSON with a bearer token in curl?
- Add an Authorization header next to the body: curl --json '{"name":"widget"}' -H "Authorization: Bearer $TOKEN" https://api.example.com/items. Keep the token in an environment variable rather than typing it into the command, so it does not end up in your shell history.