1. HTTP (Hypertext Transfer Protocol)

  1. HTTP Headers

  1. Content-Type
    1. Writing requests (clients)
    2. Reading requests (servers)
    3. Writing responses (servers)
    4. Reading responses (clients)
    5. Overview table
    6. Syntax
      1. Example

Content-Type

The Content-Type header indicates the media type of the document in the message body: the way to decode the data inside the document. The Content-Type header may include media type parameters, such as the document's character set, profile, and/or compatibility information.

Writing requests (clients)

Clients use the Content-Type header in requests when sending a message body, to inform the server on how to parse the body. Clients sending a body in a request should always use the Content-Type header, unless a specific security consern warrants otherwise, and the server only accepts a single media type at the target.

Reading requests (servers)

Use a media type parsing library to normalize the media type string.

Do not accept message bodies without a media type.

Make sure the client's indicated media type is preserved. If the media type of documents is indicated by filename extension, do not save uploads with filename extensions that conflict with the client's indicated media type. For example, if the server is configured to serve files ending in ".txt" as text/plain;charset=UTF-8, then do not accept uploads with Content-Type: text/plain;charset=ISO-8859-4, unless the server can convert the media type.

To reject uploads with an unsupported or missing media type, return 415 (Unsupported Media Type) and include a response describing the permitted media types.

Writing responses (servers)

Use the generated media type from the document serializer if it provides one; this will ensure that the correct media type (including parameters) will be written. This is especially important for conveying the correct character set to the client.

Reading responses (clients)

The Content-Type header is used to determine how to parse the final document of bytes into useful data. Each type (top type and subtype, without parameters) is described by a single specification that specifies how to read data out of the document, and what kind of data is available.

Use a parser to read the media type; top-level types, subtypes, and parameter names are all case-insensitive. Keep a map of normalized media types to parsers; then normalize the top-level and subtype, and lookup the normalized string in the parser map.

This value may be influenced by the Accept header; if this is the case, the server will usually send Vary: Accept in the response.

Overview table

Name
Content-Type
Description
Indicates the media type of the document.
Direction
Both
Negotiated by
Accept, Accept-Charset
Specification
RFC 7231: HTTP/1.1 Semantics and Content ยง3.1.1.5. Content-Type

Syntax

Content-Type = media-type

Example

Content-Type: text/html; charset=ISO-8859-4
Content-Type: application/xhtml+xml;charset=UTF-8