This browser does not support JavaScript

How to Use cURL POST to Send JSON Data (Beginner-Friendly Guide)

Proxy
OkeyProxy

You can POST JSON with cURL by adding a Content-Type header and passing your JSON body with the -d or --json option, for example:

curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' https://api.example.com

Quick answer: the one-line command

If you’re only here for the copy-paste solution, this is the fastest way to send JSON using cURL. Most developers use a simple inline payload and specify the Content-Type header. For standard Unix shells, the go-to pattern looks like this:

curl -X POST -H "Content-Type: application/json" -d '{"name":"John","age":30}' https://api.example.com/users

This works because the -X POST flag sets your method, the header tells the server your body is JSON, and -d attaches the raw payload. As Reddit’s @andrewbuilds said: “Nine times out of ten, this is exactly what people want when they say they need a curl post example.”

curl post

Common ways to send JSON with cURL

Inline JSON with -d or --data

Inline JSON is perfect for quick tests or lightweight payloads. Unix shells prefer single quotes to avoid escaping issues. Many developers mention this as the most common beginner mistake. Inline JSON is quick, clean, and ideal for simple API calls.

Sending JSON from a file using -d @file

When dealing with larger bodies, it’s easier to load JSON from a file:

curl -X POST -H "Content-Type: application/json" -d @payload.json https://api.example.com/process

On X, @jake_the_builder said loading from a file “saves you from quote-escaping hell.” It’s common when testing APIs or proxy dashboards such as OkeyProxy that return nested JSON.

Using the newer --json option

Modern cURL adds a convenient --json flag that sets method and headers automatically:

curl --json '{"email":"[email protected]"}' https://api.example.com/login

Facebook dev @melissacodes said she uses --json exclusively now because it removes boilerplate.

Platform and shell quirks that trip people up

Windows PowerShell and CMD quoting rules

PowerShell requires escaped double quotes:

curl -X POST -H "Content-Type: application/json" -d "{\"user\":\"sam\"}" https://api.example.com/login

Many devs prefer Git Bash or WSL for consistency.

Multiline JSON, stdin, and here-doc tricks

For readable payloads:

echo '{ "task": "sync", "priority": "high" }' | curl --json @- https://api.example.com/jobs

Reddit user @kevinwritescode says stdin-based posts avoid most quoting problems.

Advanced options and useful variants

Using --data-binary for exact body precision

--data-binary preserves the body exactly, useful for signature-based auth or strict servers.

Adding headers, auth tokens, and query parameters

Example:

curl -X POST -H "Authorization: Bearer YOUR_TOKEN" --json '{"ping":1}' https://api.example.com/ping

TLS and certificate notes

Use --insecure sparingly. Security engineer @ryansec warns: “If you turn it off in dev, you’ll forget in prod.”

Debugging, testing, and common server errors

Show request and response headers with -v or -i

-v reveals the full request and is essential when debugging JSON parsing issues.

Typical mistakes and how to fix them

Missing Content-Type causes 415 errors. Broken quoting or malformed JSON also trigger issues. Instagram’s @lindseycodes recommends validating JSON before sending.

Best practices and a quick security checklist

Validate JSON. Avoid exposing tokens in shell history. Prefer file-based JSON. QA teams often combine cURL testing with residential proxy IPs such as OkeyProxy to mimic real-user environments.

Quick copy-ready examples

Unix inline:

curl -X POST -H "Content-Type: application/json" -d '{"a":1}' https://api.example.com/test

PowerShell:

curl -X POST -H "Content-Type: application/json" -d "{\"a\":1}" https://api.example.com/test

File-based:

curl --json @payload.json https://api.example.com/process

Bearer token example:

curl -X POST -H "Authorization: Bearer TOKEN" --json '{"task":"run"}' https://api.example.com/run

FAQs

How do I POST JSON with cURL on Windows?

Use escaped double quotes inside the body.

Why am I getting 415 errors?

Your Content-Type header is missing or incorrect.

Can I send JSON from a file?

Yes, -d @file.json or --json @file.json works.

Difference between -d, --data-binary, and --json?

-d formats data, --data-binary preserves body, --json sets headers automatically.

Can I pipe JSON directly?

Yes: echo '{"a":1}' | curl --json @- https://... which keeps your commands tidy.

您可以通过添加 Content-Type 标头并使用 -d 或 --json 选项传递 JSON 正文,使用 cURL POST JSON,例如: