Content-Type
Media type of the request body
What the Content-Type header does
Content-Type declares the media type of the message body so the other side knows how to parse it — application/json, text/html; charset=utf-8, multipart/form-data, and so on. It appears on both requests (describing the body you send) and responses (describing the body you get back). A wrong or missing value is a common cause of 400 and 415 errors.
Example
Content-Type: application/jsonContent-Type in practice
JSON request with fetch
fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
});Common values
Content-Type: text/html; charset=utf-8
Content-Type: application/json
Content-Type: application/x-www-form-urlencoded
Content-Type: multipart/form-data; boundary=----abc123Common Content-Type questions and problems
What happens if Content-Type is missing or wrong?
The receiver has to guess. Browsers may sniff the body and treat it as a different type, and API servers often ignore or reject a body they cannot identify (400, 415). Always send an explicit type.
When do I need charset or boundary?
Text types should carry a charset, as in text/html; charset=utf-8. multipart/form-data needs a boundary parameter, which clients normally add for you. Do not set the multipart header by hand when using fetch or FormData.
Request vs response Content-Type?
On a request it describes the body you are sending. On a response it describes the body you receive. The Accept request header is the separate way to say which response types you want.
How do I stop browsers from guessing the type?
Send X-Content-Type-Options: nosniff along with the correct Content-Type. Scripts and styles with the wrong type are then blocked.
Content-Type vs related headers
- Accept: Accept says which response types the client wants. Content-Type says what the body actually is.
- Content-Encoding: Content-Encoding says how the body is compressed. Content-Type says what the data is once decompressed.
Where it's used
Content-Type is a request header. Request headers are sent by the client to describe the request and what the client can accept.