Content Type
About
The Content-Type
is an HTTP header used to indicate the media type (also called MIME type) of the data being sent in the body of an HTTP request or response. It tells the server or client how to interpret the content of the message body.
Why is Content-Type Important?
APIs often exchange data in formats like JSON, XML, HTML, plain text, etc. The Content-Type
header helps ensure that:
The server knows how to parse the incoming request body
The client knows how to handle the response body
If the Content-Type
is missing or incorrect, the receiver may fail to understand or parse the data correctly.
Usage
In a Request
When a client (like Postman, browser, or frontend app) sends data to the server, it must specify the format using the Content-Type
header.
Example:
POST /api/users
Content-Type: application/json
{
"name": "John",
"email": "[email protected]"
}
The server will use this header to determine how to read the request body — in this case, as JSON.
In a Response
When the server responds, it can also set the Content-Type
to tell the client what format the response body is in.
Example:
HTTP/1.1 200 OK
Content-Type: application/xml
<user>
<name>John</name>
<email>[email protected]</email>
</user>
Possible Content-Type Values
application/json
JSON data. Most common format in REST APIs.
application/xml
XML data. Used in older APIs and some enterprise systems.
text/plain
Plain text data with no formatting.
text/html
HTML content, used in web pages.
application/x-www-form-urlencoded
Form data encoded in key-value pairs (like key=value&key2=value2
). Default for HTML forms.
multipart/form-data
Used for form submissions that include files (e.g., image uploads).
application/octet-stream
Binary data; generic for any file (used in downloads or file streams).
application/pdf
PDF files.
application/zip
ZIP compressed files.
application/javascript
JavaScript files.
text/css
CSS stylesheets.
image/png
PNG image.
image/jpeg
JPEG image.
image/gif
GIF image.
image/svg+xml
SVG (Scalable Vector Graphics) in XML format.
audio/mpeg
MP3 audio format.
audio/wav
WAV audio format.
video/mp4
MP4 video format.
video/x-msvideo
AVI video format.
application/vnd.api+json
JSON:API specification format.
application/graphql
GraphQL query format.
application/ld+json
Linked Data using JSON (used in semantic web, schema.org).
application/soap+xml
SOAP XML format (used in SOAP-based APIs).
application/vnd.ms-excel
Microsoft Excel (XLS) file.
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Microsoft Excel (XLSX) file.
application/msword
Microsoft Word (DOC) file.
application/vnd.openxmlformats-officedocument.wordprocessingml.document
Microsoft Word (DOCX) file.
application/x-yaml
YAML format (not standard but used in APIs and config services).
application/x-protobuf
Protocol Buffers (binary serialization format).
application/x-ndjson
Newline-delimited JSON (used in streaming APIs).
Relation to Accept Header
Content-Type
is used to indicate the format being sent.Accept
is used to indicate the format expected in response.
Example:
POST /api/data
Content-Type: application/json
Accept: application/xml
This means:
"I’m sending you JSON"
"Please respond with XML"
Content-Type in API Frameworks (like Spring)
In Java Spring (or similar frameworks):
To define what our API accepts, use
consumes = MediaType.APPLICATION_JSON_VALUE
To define what our API returns, use
produces = MediaType.APPLICATION_XML_VALUE
Example:
@PostMapping(value = "/users", consumes = "application/json", produces = "application/xml")
public UserResponse createUser(@RequestBody UserRequest user) {
...
}
What Happens if Content-Type is Missing ?
The server might reject the request (
415 Unsupported Media Type
)The server might default to another format and incorrectly parse the data
The client might misinterpret the response content
Last updated