Set an HTTP content type for JSON and distinguish JSON response negotiation from the format of the sent body.

JSON Content Type: Send and Check an HTTP Exchange

Set the Content-Type header to application/json when the HTTP body contains JSON. To ask for a JSON response, also send Accept: application/json. The first header describes the body you send; the second expresses what you can receive.

Content-Type JSON requests: identify the body

The MIME type JSON uses is application/json, as specified in RFC 8259. For the standard Content-Type JSON uses, choose application/json. A request carrying that header should contain valid serialized JSON. A response with the same header likewise identifies its own body as JSON. The header does not transform an object into text or validate the result.

For example, Content-Type: application/json on a POST tells the server how to interpret the request body. On the response, the header tells the client how to interpret the returned representation. A JSON content type does not describe the values inside the document: the endpoint’s contract defines required fields and their meanings.

Accept has a different role. It tells the server which response media types the client can handle. The server selects a representation according to its implementation and tells the client what it sent in the response’s Content-Type. Sending Accept: application/json alone does not make a request body JSON, and a client should inspect the actual response rather than assuming the server honored its preference.

A request and response to inspect

Imagine an API endpoint that creates a note. This illustrative exchange shows both directions; replace the host and path with your own endpoint when testing:

POST /notes HTTP/1.1
Host: api.example.com
Content-Type: application/json
Accept: application/json

{"title":"Check headers"}
HTTP/1.1 201 Created
Content-Type: application/json

{"id":42,"title":"Check headers"}

The request’s body is a JSON object with a string property. The response carries a separate JSON object and a status indicating creation. Your API may return a different status, structure, or even no body; follow its documented contract. When comparing a real exchange, check the endpoint and status as well as the two message bodies. A content type identifying JSON is useful only when the bytes match it. In JSON, object keys and string values use double quotes, so {title: 'Check headers'} is a JavaScript object literal, not valid JSON text.

Serialize before sending

In JavaScript, use JSON.stringify() to turn an ordinary object into a JSON string. This example sends the same data with fetch:

const response = await fetch('/notes', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json'
  },
  body: JSON.stringify({ title: 'Check headers' })
});

if (!response.ok) throw new Error(`HTTP ${response.status}`);
const note = await response.json();
console.log(note.id);

This assumes a successful endpoint response contains JSON. fetch() resolves even for HTTP error statuses, which is why the example checks response.ok first. If a successful endpoint can return an empty body, handle that case before calling response.json(). For networked exchange, encode JSON as UTF-8. Serialization also has limits: for example, properties with undefined values are omitted from objects, so inspect the result when that distinction matters to the API.

Diagnose a mismatch

In your browser’s Network panel or an HTTP client, inspect the request headers and payload, then the response status, headers, and body. A 415 Unsupported Media Type can indicate that the endpoint rejects the request’s declared format. Compare its expected media type with the request header and send correctly serialized data. A JSON parsing error can instead mean the response is empty, malformed, or HTML from an error page. Check the actual content type of the JSON response and its body before changing the parser or server configuration.