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)
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 transferDownlink
— approximate bandwidth of the client's connection to the server in MbpsECT
— effective connection type; one ofslow-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
- MIME types (IANA media types) » MIME sniffing (MDN) — https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#mime_sniffing
- Client Hints (MDN) — https://developer.mozilla.org/en-US/docs/Glossary/Client_hints
References
- Content negotiation (MDN) — https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation
- Quality values (MDN) — https://developer.mozilla.org/en-US/docs/Glossary/Quality_values
- Content-Type (MDN) — https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type
- Content-Encoding (MDN) — https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding
- Content-Language (MDN) — https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Language
- Content-Location (MDN) — https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Location
- Accept-Post (MDN) — https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Post
- Accept-Patch (MDN) — https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Patch
- Accept-CH (MDN) — https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-CH
- X-Content-Type-Options (MDN) — https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options
- Accept (MDN) — https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept
- Accept-Encoding (MDN) — https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding
- Accept-Language (MDN) — https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language