POST JSON data
Three pieces: -X POST for the method, -H "Content-Type: application/json" so the server parses the body as JSON, and -d for the payload:
curl -X POST https://example.com/api/users \
-H "Content-Type: application/json" \
-d '{"name": "Ada", "role": "admin"}'
Use single quotes around the JSON so your shell leaves the double quotes inside it alone. (Note: -d implies POST, so -X POST is technically optional — but it's good to keep it explicit.)
The --json shortcut (curl 7.82.0+)
Modern curl has a dedicated flag that replaces the -X/-H/-d trio. --json sets Content-Type: application/json and Accept: application/json, and sends the body:
curl --json '{"name": "Ada", "role": "admin"}' https://example.com/api/users
Check your version with curl --version; if it's older than 7.82.0, use the explicit -H/-d form above.
Sending JSON from a file
For large or reusable payloads, put the JSON in a file and reference it with @:
curl -X POST https://example.com/api/users \
-H "Content-Type: application/json" \
-d @payload.json
--json @payload.json works the same way on curl 7.82.0+. Use --data-binary @file.json if you need to preserve newlines exactly (-d strips them).
GET and parse a JSON response
To fetch JSON, ask for it with an Accept header and pipe the output to jq for readable, queryable output:
# Pretty-print the whole response
curl -s -H "Accept: application/json" https://example.com/api/users | jq
# Pull out one field
curl -s -H "Accept: application/json" https://example.com/api/users/1 | jq '.email'
-s silences the progress meter so only the JSON reaches jq.
Common JSON gotchas
- Forgetting
Content-Type. Without it,-ddefaults toapplication/x-www-form-urlencodedand many APIs reject the body or fail to parse it. - Shell quoting. Double quotes inside double quotes break the payload — wrap JSON in single quotes, or read from a file.
- Getting HTML instead of JSON. If an API returns an HTML block page rather than JSON, you're being filtered as a bot — the issue is your fingerprint/IP, not your curl syntax. See the code example for routing JSON calls through a scraping API.
