Commandcurl

POST JSON to an API

Sends a JSON request body to an API endpoint with the right Content-Type header set, so the server parses it as JSON instead of form data.

Last updated

Commandcurl
curl -X POST https://api.example.com/items -H "Content-Type: application/json" -d '{"name":"widget"}'

How it works

-H "Content-Type: application/json" tells the server how to interpret the request body — without it, many frameworks assume the default application/x-www-form-urlencoded and either reject a JSON payload outright or silently fail to parse it, since curl doesn't set this header on its own just because the body looks like JSON. -d supplies that body; curl switches its request method to POST automatically the moment -d is present, which is why -X POST reads as slightly redundant here but is kept explicit anyway.

Keeping -X POST explicit, even though curl would infer it, pays off the moment you swap this into a PUT or PATCH request later — those methods aren't inferred from anything, so the habit of always stating -X avoids a confusing silent fallback to GET or POST when you meant something else.

Watch out for

  • -X POST is technically inferred once -d is present, but dropping -d without also removing -X POST sends an empty POST request with no body — a common source of "why is my API getting nothing" confusion.
  • The JSON payload is wrapped in single quotes here so the shell doesn't try to interpret the double quotes or any $variables inside it. On Windows cmd.exe (not PowerShell or WSL), quoting rules are different — wrap the JSON in double quotes instead and escape the inner double quotes with backslashes.
  • For a payload too large or awkward to inline, use -d @payload.json to read the request body from a file instead of typing it on the command line.

Related in Commands