How do I POST JSON data with cURL?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Posting JSON with curl is mostly about sending the correct request body and telling the server what format that body uses. The minimal pattern is to set the Content-Type header to application/json and pass the JSON document with -d.
Basic POST Request
A standard JSON request looks like this in a Unix shell:
These flags do the important work:
- '
-X POSTchooses the HTTP method' - '
-Hsets a request header' - '
-dsends the request body'
In many cases curl infers POST automatically when -d is present, but writing -X POST makes the command explicit and easier to read.
Send JSON From A File
Inline JSON is fine for small payloads, but it gets messy once the document grows. A file is more maintainable.
Create data.json:
Then send it with:
The @ prefix tells curl to read the body from a file instead of from the command line.
Include Authentication Headers
APIs often require a token or API key in addition to the JSON body.
This pattern works for most bearer-token APIs. If the server expects another header format, keep the JSON body the same and change only the authentication header.
See The Request And Response Details
When a server rejects your JSON, the fastest next step is to make curl verbose.
Verbose mode shows request headers, response headers, TLS negotiation details, and the status line. That is usually enough to tell whether the problem is authentication, content type, routing, or malformed JSON.
Windows Quoting Differences
On Windows, shell quoting can trip people up. In PowerShell, single quotes are often the easiest way to protect JSON containing double quotes.
If quoting becomes painful, use a file and -d @data.json instead. That removes most shell-specific escaping issues.
Content-Type Versus Accept
Content-Type tells the server what you are sending. Accept tells the server what response format you would like back.
You often want both, but only Content-Type is required to describe the JSON request body.
Common Pitfalls
The most common mistake is forgetting the Content-Type: application/json header. Some servers then treat the body as plain form data or reject it outright.
Another mistake is malformed shell quoting. If the shell changes your quotes before curl sees them, the server receives broken JSON. Using a file is the safest fix.
A third issue is confusing -d with multipart upload options such as -F. -F builds a multipart/form-data request, which is different from a raw JSON body.
Summary
- Use
-H "Content-Type: application/json"and-dto send JSON withcurl. - Inline JSON works for small payloads, but
@file.jsonis easier to maintain. - Add authentication headers separately when the API requires them.
- Use
-vto debug rejected requests. - If shell quoting is a problem, move the JSON into a file and post that file.
- '
Accept: application/jsoncan help when an API varies its response format.'
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.