415 Unsupported Media Type

The server refuses the request because the body is in a format this endpoint does not accept for this method. Nearly always, the Content-Type header is missing or wrong.

What 415 means

A request with a body should say what the body is, in its Content-Type header. A 415 means the server looked at that declaration, or at the Content-Encoding, or at the data itself, and found a format it does not handle here.

The check happens before the body is parsed. That is the difference from 400: a 415 says "I do not take XML", a 400 says "I take JSON and this is not valid JSON".

The server can help by listing what it would have accepted. For a media type problem it can send an Accept header in the response; for a compression problem, Accept-Encoding.

The usual culprits

  • curl with -d. It sends Content-Type: application/x-www-form-urlencoded unless told otherwise, even when the data is JSON. Add -H "Content-Type: application/json", or use --json in recent versions.
  • fetch with a string body. body: JSON.stringify(data) without a headers entry goes out as text/plain;charset=UTF-8.
  • No Content-Type at all, from a hand-built request or a minimal HTTP client.
  • A near miss. text/json for application/json, a vendor type such as application/vnd.api+json that the API requires and you did not send, or a charset parameter a strict server will not match.
  • A compressed body. Content-Encoding: gzip on a request to a server that only reads uncompressed bodies.
  • The wrong kind of upload. JSON sent to an endpoint that expects multipart/form-data, or the reverse.

Fixing it as a client

curl -X POST https://api.example.com/orders \
  -H "Content-Type: application/json" \
  -d '{"sku":"A-100","qty":2}'
  • Look at the request that actually left: curl -v, or the Network tab in browser developer tools. The header you set in code is not always the one that is sent.
  • Read the 415 response's headers for Accept, and the API documentation for the required type.
  • For multipart/form-data, let the HTTP library set the header. It has to include a boundary string that matches the body, and a hand-written header will not have one.
  • Do not mix up Accept and Content-Type in the request. Accept describes what you want back; Content-Type describes what you are sending.

Sending 415 from your own API

  • Return it when the declared type is one you do not support, and name the supported types in the body or an Accept response header.
  • Compare the media type only, ignoring parameters and letter case, so that application/json; charset=utf-8 is accepted.
  • Decide what a missing Content-Type means. Guessing is friendly; rejecting with 415 is stricter and safer. Do not fall through to a 500 from a parser that received something unexpected.

415 compared with 406, 400 and 422

CodeProblem withHeader to check
415 Unsupported Media TypeThe format of the request bodyContent-Type, Content-Encoding (request)
406 Not AcceptableThe format you asked for in the responseAccept (request)
400 Bad RequestA body in the right format that does not parseNone; fix the body
422 Unprocessable ContentA body that parses but has invalid valuesNone; fix the data

Example 415 response

HTTP/1.1 415 Unsupported Media Type
Accept: application/json
Content-Type: application/json
Content-Length: 80

{"error":"unsupported_media_type","message":"Send the body as application/json"}

Accept is normally a request header. In a 415 response it lists the media types the server would have accepted in the request.

Defined in RFC 9110, section 15.5.16.

Questions

How do I fix a 415 Unsupported Media Type error?

Set the Content-Type request header to the format the API expects, most often application/json, and make sure the body really is in that format. Check the request as sent, because tools such as curl and fetch add a different default when you do not set one.

Why does curl return 415 when I post JSON?

With -d, curl labels the body application/x-www-form-urlencoded. Add -H "Content-Type: application/json" to the command, or use the --json option, which sets the header for you.

What is the difference between 415 and 406?

A 415 is about what you sent: the server does not accept the request body's format. A 406 is about what you asked for: the server cannot produce a response in any format listed in your Accept header.

Should an API return 415 or 400 for a wrong Content-Type?

415 is the precise answer when the declared format is unsupported. Use 400 when the format is supported but the body is malformed. Some frameworks return 400 for both, which is acceptable as long as the error message is clear.