Practical guide · APIs and web applications

Understanding JSON Content Types in APIs and Web Applications

Learn which HTTP media type identifies JSON, how request and response headers work, and what to set in common API clients and frameworks.

JSON is a common format for sending structured data between browsers, APIs, and services. The HTTP header that identifies a JSON message body is Content-Type: application/json. Using the right media type helps clients and servers select the expected parser and handle the payload consistently.

What does Content-Type mean?

Content-Type describes the media type of the representation in the current HTTP message body. For example, a request with a JSON body can include:

Content-Type: application/json

The header tells the server how the request body is formatted. A response can also include Content-Type so the client knows how to interpret the returned body.

Use application/json for JSON

The registered media type for JSON data is application/json. Use it for JSON API requests and responses instead of older or unrelated types such as text/json or application/javascript.

POST /api/users HTTP/1.1
Content-Type: application/json
Accept: application/json

{
  "name": "Suresh",
  "role": "Developer"
}

Here, Content-Type describes the JSON request body. Accept expresses the response media types the client is willing to receive. They describe different directions, so an API client may set both.

Request headers and response headers

A client sending JSON should identify its body with Content-Type. A server that returns JSON should identify the response the same way:

HTTP/1.1 200 OK
Content-Type: application/json

{
  "status": "success",
  "message": "User created"
}

Clients and server frameworks can then choose a JSON parser or serializer. A successful response is not guaranteed to have a body, though; for example, a 204 No Content response should not be parsed as JSON.

Should you add charset=utf-8?

You may see headers such as Content-Type: application/json; charset=utf-8. JSON exchanged between open systems uses UTF-8. The JSON media type does not require a charset parameter, so it is usually sufficient to send application/json. Follow a specific API's documented behavior if it requires a particular header format.

Send JSON with JavaScript Fetch

Serialize a JavaScript value with JSON.stringify(), set the request's content type, and inspect the response status before parsing its body:

const response = await fetch("/api/users", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "Accept": "application/json"
  },
  body: JSON.stringify({ name: "Suresh", role: "Developer" })
});

if (!response.ok) {
  throw new Error(`Request failed: ${response.status}`);
}

const result = await response.json();

Setting Content-Type does not serialize the object by itself; the body still needs to be a JSON string. For cross-origin requests, the browser may perform a CORS preflight depending on the request.

Use Axios

Axios serializes plain JavaScript objects as JSON in common configurations. You can set the desired headers explicitly when needed:

const response = await axios.post(
  "/api/users",
  { name: "Suresh", role: "Developer" },
  {
    headers: {
      "Content-Type": "application/json",
      "Accept": "application/json"
    }
  }
);

Check your Axios version and any configured interceptors if the request uses a custom transform or content type.

Use Python Requests

Pass a Python object with the json argument. Requests encodes it as JSON and sets the JSON content type automatically:

import requests

response = requests.post(
    "https://api.example.com/users",
    json={"name": "Suresh", "role": "Developer"},
    headers={"Accept": "application/json"},
    timeout=10,
)
response.raise_for_status()
data = response.json()

Use data= when you are intentionally sending a pre-serialized body or another form of payload; in that case, set the matching Content-Type yourself.

Frameworks can set the response type

ASP.NET Core

Returning an object from a controller action such as return Ok(data) is normally serialized as JSON by ASP.NET Core's configured output formatter, with an appropriate response content type.

Express

Use res.json() to serialize and send a JSON response. For JSON request bodies, install the built-in JSON parser middleware before the route that reads the body:

app.use(express.json());

app.post("/api/users", (req, res) => {
  res.json({ received: req.body });
});

Middleware and formatters vary by framework and configuration; verify the headers your API actually sends.

Common mistakes

Best practices

Quick reference: Content-Type: application/json says “this message body is JSON.” Accept: application/json says “I can accept a JSON response.” Use both when they match your request and response needs.