Curl Content-Type Example

Content-Type tells the server how to parse the body you sent. Curl fills it in for you based on the flag you used, and its guess is right less often than people expect.

Last updated . How this page is checked.

The command

curl https://httpbin.org/post \
  -H "Content-Type: application/json; charset=utf-8" \
  -d '{"city":"Zürich"}'

Opens the request console with this request filled in. It is sent from api-tester.com's server rather than from your machine, so it shows what the endpoint returns, not how your own network or curl build behaves.

What the flags do

FlagWhat it does
-d / --data-binaryDefault type application/x-www-form-urlencoded, whatever the data is.
--jsonSets application/json.
-FSets multipart/form-data; boundary=.... Leave it alone.
-TSets no Content-Type at all.
-H "Content-Type: ..."Overrides any of the above.

What curl sends by default

The default comes from the flag, not from the content. Curl does not look inside the body, so a JSON string given to -d is announced as a form, and a PNG sent with --data-binary is too. The server then runs the wrong parser and reports an empty or malformed body, or refuses outright with 415 Unsupported Media Type.

Set the header explicitly whenever the body is anything other than form fields.

curl -d '{"a":1}' https://httpbin.org/post                        # says form — wrong
curl -d '{"a":1}' -H "Content-Type: application/json" https://httpbin.org/post
curl --data-binary @data.csv -H "Content-Type: text/csv" https://httpbin.org/post

Charset and parameters

A media type can carry parameters after a semicolon. charset=utf-8 is the common one; it matters for text formats such as text/plain, text/csv and XML. JSON is defined as UTF-8 already, so the parameter is redundant there, though harmless and often expected by older servers.

Check what was really sent with -v, and remember that Content-Type on the response is a separate header describing what came back.

curl -v -o /dev/null -d "note=hi" https://httpbin.org/post 2>&1 | grep -i "^> content-type"

Common mistakes

  • Content-Type on a GET is meaningless. To choose the response format, send Accept.
  • Overriding the type on a -F request deletes the boundary and breaks the upload.
  • Typos fail silently. application/jsno is a valid-looking header that no server will parse as JSON.

Related curl examples

More under Headers

References

These links leave api-tester.com, which is independent of the projects they point to.

FAQ

How do I set the Content-Type header in curl?

Add -H "Content-Type: type/subtype", for example -H "Content-Type: application/json". It overrides the type curl would send by default.

What is the default Content-Type in curl?

application/x-www-form-urlencoded when a body is sent with -d or --data-binary, application/json with --json, and multipart/form-data with -F. Requests without a body have none.

How do I fix 415 Unsupported Media Type with curl?

Set the Content-Type header to the media type the API expects for the body you are sending, most often application/json.