Content Negotiation

With content negotiation, a server can serve different representations ("formats") of a resource at the same URI (MDN).

The client can tell the server which representation it supports/prefers via Accept-* request headers. The server indicates which representation it sent via Content-* response headers. (In some cases, the role is reversed.)

You can also use this for compression (e.g. Accept-Encoding: gzip).

Terminologies

Quality value

In some of the directives below, you may attach a "quality value" (;q=) from 0 up to 1. It's basically the priority of that item, the default being 1.

Also, if they have the same quality value, more specific items (like text/html) will have more priority over the less specific ones (like text/* or */*).

Accept: text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8

Client Hints (CH)

Currently, this is experimental.

Client Hints are request headers sent by the client to give information about the device, network, user, and the user agent.

These headers include:

  • Device-Memory amount of available memory in GB (rounded to a power of 2)
  • Save-Data indicates preference to save/reduce data transfer
  • Downlink approximate bandwidth of the client's connection to the server in Mbps
  • ECT effective connection type; one of slow-2g, 2g, 3g, 4g
  • RTT approximate round-trip time on the application layer, in ms

Response headers

Content-Type

Indicates the original MIME type of the resource sent by the server (prior to any content encoding). This is the server's reply to the Accept request header.

Browsers may ignore this value and detect the MIME type themselves (see X-Content-Type-Options to disable this behavior).

Content-Type: text/html;charset=UTF-8
Content-Type: multipart/form-data; boundary=<boundary_string>

Content-Encoding

Lists any encoding(s) that have been applied (usually compression algorithms). This is the server's reply to the Accept-Encoding request header.

Content-Encoding: gzip
Content-Encoding: compress
Content-Encoding: deflate
Content-Encoding: br

Content-Language

Describes the language(s) of the intended audience. This is the server's reply to the Accept-Language request header.

This may not be the language of the document itself (e.g. Content-Language: de-DE may be written in English if it's a language course page for German speakers). For that, use lang attribute instead.

Content-Language: en-US
Content-Language: de-DE
Content-Language: de-DE, en-CA

Content-Location

Indicates the direct URL to access the resource without content negotiation.

Content-Location: <url>

Accept-Post

Advertises which MIME types are accepted for HTTP POST requests.

Accept-Post: application/example, text/example
Accept-Post: image/webp
Accept-Post: */*

Accept-Patch

Advertises which MIME types are accepted for HTTP PATCH requests.

Accept-Patch: application/example, text/example
Accept-Patch: text/example;charset=utf-8
Accept-Patch: application/merge-patch+json

Accept-CH

Indicates which client hint header(s) a client should include in subsequent requests.

(If a client hint header determines which representation is sent, you should also include it in the Vary header to ensure correct caching behavior.)

Accept-CH: Viewport-Width, Width

X-Content-Type-Options

Sometimes, browsers may do "MIME-type sniffing" (deducing a content's MIME type themselves by looking at the content). To prevent this behavior, you can specify X-Content-Type-Options: nosniff.

X-Content-Type-Options: nosniff

Request headers

Accept

Lists the MIME types the client understands.

You can also give "quality values" to denote priorities in the list, e.g. Accept: text/html, application/xml;q=0.9.

Accept: <MIME_type>/<MIME_subtype>
Accept: <MIME_type>/*
Accept: */*

Accept-Encoding

Lists which content encoding (usually compression algorithms) the client understands.

You can also give "quality values" to denote priorities in the list, e.g. Accept-Encoding: deflate, gzip;q=1.0, *;q=0.5.

Accept-Encoding: gzip
Accept-Encoding: compress
Accept-Encoding: deflate
Accept-Encoding: br
Accept-Encoding: identity
Accept-Encoding: *

Accept-Language

Lists which language the client understands.

You can also give "quality values" to denote priorities in the list, e.g. Accept-Language: fr-CH, fr;q=0.9, en;q=0.8

Accept-Language: <language>
Accept-Language: *

See also

References