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
- Sending a JSON string without a content type: many servers rely on
Content-Typeto choose the request parser. - Using
text/plainfor a JSON API body: the body may look like JSON text, but the server may not route it through its JSON parser. - Confusing
AcceptandContent-Type: the first describes acceptable response formats; the second describes the current message body. - Passing an object directly to Fetch: call
JSON.stringify()before using the value as a JSON request body. - Treating the header as validation or security: a correct media type improves interoperability, but servers must still validate data, authorize requests, and handle untrusted input safely.
- Parsing an empty response as JSON: check the response status and whether a body is expected before calling a JSON parser.
Best practices
- Use
application/jsonconsistently for JSON API request and response bodies. - Set
Accept: application/jsonwhen the client specifically expects a JSON response. - Use UTF-8 and follow the API's documented serialization and header requirements.
- Validate and authorize incoming data on the server; a content type alone does not make a payload safe.
- Check actual response headers and status codes when debugging parsing or negotiation issues.
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.