Skip to content
On this page

To POST JSON with curl, set the content type and pass the body: curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' https://example.com/api. Since curl 7.82.0 there's a shortcut — --json — that sets both the Content-Type and Accept headers to application/json and sends the body in one flag. To read JSON back, request it with an Accept header and pipe the response to jq.

Quick facts

POST JSON-X POST -H "Content-Type: application/json" -d '{...}'
Shortcut (curl 7.82+)--json '{...}'
From a file-d @data.json
Read JSON back-H "Accept: application/json" … | jq
GotchaWithout Content-Type the server may reject or misparse the body

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, -d defaults to application/x-www-form-urlencoded and 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.

Code example

bash
# POST JSON (classic, works on every curl version)
curl -X POST https://example.com/api/users \
  -H "Content-Type: application/json" \
  -d '{"name": "Ada", "role": "admin"}'

# POST JSON (curl 7.82.0+ shortcut — sets Content-Type AND Accept)
curl --json '{"name": "Ada", "role": "admin"}' https://example.com/api/users

# POST JSON from a file
curl -X POST https://example.com/api/users \
  -H "Content-Type: application/json" -d @payload.json

# GET JSON and parse it with jq
curl -s -H "Accept: application/json" https://example.com/api/users | jq '.[].email'

Next in Web fundamentals for scrapers · 7 of 7

And the flag you will eventually need for debugging.

How to Make curl Ignore SSL Certificate Errors

Related terms

Concept map

Concept map

How How to Send and Receive JSON with curl connects

The terms most directly tied to this one. Hover a node to see its neighbours, click to preview, drag to rearrange.

0 terms · 0 connections
You are here · Web Technologies
Building map…

Frequently asked questions

How do I POST JSON with curl?

Use curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' https://example.com/api. The Content-Type header tells the server to parse the body as JSON. On curl 7.82.0+ you can use the --json shortcut instead, which sets both Content-Type and Accept.

What does the --json flag do in curl?

Added in curl 7.82.0, --json '{...}' is a shortcut that sends the JSON body and sets both Content-Type: application/json and Accept: application/json in one flag, replacing the -X POST, -H, and -d combination.

How do I send JSON from a file with curl?

Reference the file with an @ prefix: curl -X POST -H "Content-Type: application/json" -d @data.json https://example.com/api. Use --data-binary @data.json if you need to preserve newlines exactly, since -d strips them.

How do I get and read a JSON response with curl?

Request JSON with -H "Accept: application/json" and pipe the response to jq for pretty-printing or field extraction: curl -s -H "Accept: application/json" https://example.com/api | jq '.field'. The -s flag hides the progress meter so only JSON reaches jq.

Last updated: 2026-06-08