The Basic Command
To send JSON, give curl the data with -d and tell the server it is JSON with a Content-Type header:
curl -X POST https://api.example.com/users \
-H "Content-Type: application/json" \
-d '{"name":"Emily","age":30}'-d already makes the request a POST, so -X POST is optional. The single quotes around the JSON keep the shell from touching the double quotes inside it.
Don't Forget the Content-Type Header
Without -H "Content-Type: application/json", curl labels the body as a web form. We sent -d '{"name":"Emily"}' with no header to a test server, and it received:
Content-Type: application/x-www-form-urlencoded
Body: {"name":"Emily"}The body is fine, but many frameworks (Express, Flask, Spring, ASP.NET) only parse JSON when the header says so. The usual symptom is an empty body on the server, a 400 Bad Request or a 415 Unsupported Media Type.
The Shortcut: --json
curl 7.82 and newer have --json, which sends the data as a POST and sets both headers for you:
curl --json '{"name":"Emily","age":30}' https://api.example.com/usersContent-Type: application/json Accept: application/json
Check your version with curl --version. Windows 10 and 11 include curl as curl.exe; older versions may not have --json yet, so use -H and -d there.
Send JSON from a File
For anything longer than one line, put the JSON in a file and add @ before the file name. This also avoids every quoting problem.
{
"name": "Emily"
}curl -H "Content-Type: application/json" --data-binary @user.json https://api.example.com/users # or, with curl 7.82+ curl --json @user.json https://api.example.com/users
Prefer --data-binary over -d for files. -d @file removes the line breaks: our test server received { "name": "Emily"} with -d, but the file exactly as written with --data-binary. For JSON both still parse, but the exact bytes matter if the API checks a signature of the body. --json keeps the line breaks too. To read the JSON from another command, use @- for standard input: cat user.json | curl --json @- URL.
PUT, PATCH and Authorization Headers
curl -X PUT https://api.example.com/users/1 \
-H "Content-Type: application/json" \
-d '{"age":31}'curl --json '{"name":"Emily"}' \
-H "Authorization: Bearer YOUR_TOKEN" \
https://api.example.com/usersUse -X PATCH the same way for partial updates. Add as many -H lines as the API needs.
Sending JSON with curl on Windows
This is where most "invalid JSON" errors come from. The single-quote command above does not survive the Windows shells. We ran it on Windows 11 against a test server:
| Shell | What the server received | Result |
|---|---|---|
| PowerShell 5.1, single quotes | {name:Emily,age:30} (the double quotes were removed) | Expected property name or '}' in JSON at position 1 |
| cmd.exe, single quotes | '{name:Emily}' (single quotes kept, double quotes removed) | Unexpected token ''', "'{name:Emily}'" is not valid JSON |
Escape every double quote inside the JSON with a backslash. This worked in both shells:
curl.exe -H 'Content-Type: application/json' -d '{\"name\":\"Emily\",\"age\":30}' https://api.example.com/userscurl.exe -H "Content-Type: application/json" -d "{\"name\":\"Emily\",\"age\":30}" https://api.example.com/usersTwo more Windows details. In Windows PowerShell 5.1, curl is an alias for Invoke-WebRequest, which takes different options, so type curl.exe. And the easiest fix of all is the file method above (--data-binary @user.json), which needs no escaping in any shell. PowerShell 7.3 and newer pass the quotes through as written, so single quotes work there.
In PowerShell you can also skip curl and let PowerShell build the JSON (more inJSON in PowerShell):
$body = @{ name = 'Emily'; age = 30 } | ConvertTo-Json
Invoke-RestMethod -Uri https://api.example.com/users -Method Post -ContentType 'application/json' -Body $bodyRead and Check the Response
# show the status line and response headers
curl -i --json '{"a":1}' https://api.example.com/users
# print only the status code
curl -s -o /dev/null -w "%{http_code}\n" --json '{"a":1}' https://api.example.com/users
# pretty print the JSON response (Python is often already installed)
curl -s --json '{"a":1}' https://api.example.com/users | python -m json.toolOn Windows, use NUL instead of /dev/null in cmd.exe. You can also paste the response into the JSON Formatter to read it as a tree.
Common Problems
- Server says the body is empty or not JSON: the
Content-Type: application/jsonheader is missing. - "Unexpected token" or "Expected property name" from the server: the JSON was mangled by shell quoting, usually on Windows. Use a file or escape the quotes. See Unexpected Token in JSON for every version of the message.
- The JSON itself is invalid: a trailing comma or single quotes inside the JSON. Check it in the JSON Validator before sending.
- The value contains quotes or line breaks: escape it as a JSON string first with JSON Escape.
Frequently Asked Questions
How do I send JSON with curl?
Use -d with the JSON and add the header -H "Content-Type: application/json", for example: curl -H "Content-Type: application/json" -d '{"name":"Emily"}' https://api.example.com/users. With curl 7.82 or newer, curl --json '{"name":"Emily"}' URL does the same.
Why does the server not see my JSON body?
Without the Content-Type: application/json header, curl sends the body as application/x-www-form-urlencoded, and many frameworks then ignore it or return 400 or 415. Add the header or use --json.
Why does curl send invalid JSON on Windows?
Windows PowerShell 5.1 strips the double quotes inside single-quoted JSON, and cmd.exe does not treat single quotes as quotes at all. Escape each double quote as \" or send the JSON from a file with --data-binary @file.json.
How do I send a JSON file with curl?
Put @ before the file name: curl -H "Content-Type: application/json" --data-binary @data.json URL, or curl --json @data.json URL. --data-binary keeps the file exactly as written, while -d removes line breaks.