Content Negotiation: How Servers Pick a Format or Language
See how Accept headers and quality values let one address serve different formats, languages and encodings without confusion.
Key takeaways
- Clients state preferences with
Accept,Accept-EncodingandAccept-Language. - Quality values from 0 to 1 rank those preferences, and 0 means "do not send this".
- The server labels its choice with headers such as
Content-Type,Content-EncodingandContent-Language. - A negotiated response should carry a
Varyheader so caches do not serve the wrong version. - For languages, separate URLs and a visible switcher usually work better than negotiation alone.
Content negotiation is how a client and server agree on which version of something to send. The browser lists what it can handle and what it prefers in Accept headers. The server picks the best match it has, labels the response with what it chose and tells caches which request headers influenced the choice.
You rely on it constantly. It is why your browser receives compressed pages, why one image address can return a newer format to capable browsers and why an API can offer more than one data format.
The basic exchange
A request might say:
GET /report HTTP/1.1
Host: www.example.com
Accept: text/html, application/json;q=0.8
Accept-Language: fr-CA, fr;q=0.9, en;q=0.6
Accept-Encoding: br, gzip And the answer:
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Content-Language: fr
Content-Encoding: br
Vary: Accept, Accept-Language, Accept-Encoding The server weighed what was asked against what it had. It had no Canadian French page, so it served general French, in HTML, compressed with Brotli.
This approach, where the server decides, is called proactive or server-driven negotiation. The alternative, reactive negotiation, has the server offer a list and let the client or user pick, which in practice mostly means links on a page.
Quality values
The q parameter assigns a weight from 0 to 1, with up to three decimal places. An item without one counts as 1.
Accept: text/html, application/xml;q=0.9, image/avif, */*;q=0.8 Here HTML and AVIF are most wanted, XML comes next and anything else is acceptable at a lower rank. */* matches every media type, and image/* matches every image type.
When several entries could match one format, the most specific one decides its weight. In text/*;q=0.5, text/html;q=0.9, HTML scores 0.9 and plain text 0.5. A value of q=0 explicitly refuses something. You can test combinations in the Accept media negotiation explorer.
Media types
A media type has a type and subtype, such as text/css, application/json or video/mp4, sometimes with parameters like charset=utf-8.
APIs use Accept to let clients choose between formats, for example JSON or CSV from the same address. Some also use custom media types to select an API version. This is tidy in theory, and harder to test than a version in the path, so weigh your audience.
Two headers are easy to confuse. Accept says what the client wants back. Content-Type on a request says what the client is sending. If a server cannot read what you sent, it should answer 415 Unsupported Media Type. If it cannot produce anything you accept, it may answer 406 Not Acceptable, though many servers sensibly send their default.
Compression
Accept-Encoding is the most widely used negotiation. Browsers advertise gzip, deflate, br and, in many current versions, zstd. The server compresses with one it supports and declares it in Content-Encoding.
This applies to text such as HTML, CSS, JavaScript, JSON and SVG. Photos, video and ZIP files are already compressed and gain little. Compression with gzip and Brotli covers server setup.
Note the distinction: Content-Type describes the thing itself, while Content-Encoding describes a wrapper that the client removes before using it.
Image formats
Newer formats such as WebP and AVIF can be much smaller than older ones at similar visual quality. A server or content delivery network can read Accept, see image/avif, and return that version from the same .jpg address.
The other method puts the choice in the page:
<picture>
<source srcset="photo.avif" type="image/avif">
<source srcset="photo.webp" type="image/webp">
<img src="photo.jpg" alt="A router on a shelf">
</picture> Here the browser picks, and each file has its own address, which makes caching straightforward. Server-side negotiation avoids touching your markup but needs cache support. See image formats for speed, or create test files with the WebP converter.
Language
Accept-Language comes from the browser or operating system settings. It lists language tags such as en-US, es or pt-BR.
It is a useful hint with real limits. People use borrowed or shared computers, keep defaults they never chose or want to read a page in a second language. Guessing from the visitor's IP address is weaker still, since location is not language.
A reliable design looks like this:
Give each language its own URL, for example /fr/ and /es/, and link the alternates with hreflang annotations so search engines can find them. Redirecting everyone automatically based on headers or location can hide versions from crawlers and trap users.
Mark the page itself too, with the lang attribute on the HTML element, which screen readers use to choose pronunciation.
Caching and the Vary header
Negotiation creates a hazard. A shared cache that stores the French, Brotli-compressed version under /report must not give it to someone who wants English and cannot decode Brotli.
Vary solves this by naming the request headers that affected the response. The cache stores variants separately and reuses one only when those headers match.
| Response varies by | Header to send | Cache effect |
|---|---|---|
| Compression | Vary: Accept-Encoding | Few variants, works well |
| Image format | Vary: Accept | Browser strings differ, so variants multiply |
| Language | Vary: Accept-Language | Many different values |
| Device by user agent | Vary: User-Agent | Shared caching nearly useless |
Content delivery networks often normalize headers, reducing the long Accept-Language string to one of your supported languages before looking in the cache. Some ignore parts of Vary unless configured otherwise, so read the documentation and test. How a CDN works gives the background.
ETag values should differ between variants as well, so that revalidation does not confirm the wrong copy.
A worked example
Say your server can produce a product listing as HTML or JSON, and it receives these three requests. The values are only an illustration.
Accept sent | HTML score | JSON score | Served |
|---|---|---|---|
text/html, */*;q=0.8 | 1.0 | 0.8 | HTML |
application/json | none | 1.0 | JSON |
*/* | 1.0 | 1.0 | Server's default |
image/png | none | none | 406, or the default |
The third row is where designs go wrong. A tie, or a bare */*, gives the client no opinion, so your server needs a written order of preference. Command-line tools commonly send exactly that.
The negotiation can also weigh the server's own view. If two formats score equally for the client, the server may prefer the one that is cheaper to produce or smaller to send. The standard leaves that judgment to the server.
Setting it up
Many web servers and frameworks handle the common cases for you. Frameworks often let you declare the formats an endpoint can produce and then match them to Accept. For compression, you usually enable a module and list the content types it should cover.
For precompressed files, a build step produces app.js.br and app.js.gz beside app.js, and the server sends the matching one with the original's Content-Type, the right Content-Encoding and Vary: Accept-Encoding. This saves processor time and allows stronger compression settings than you would use on the fly.
When you write the matching yourself, resist searching the header for a substring. A header containing image/avif;q=0 mentions AVIF while refusing it. Use a library that parses weights and specificity.
Offering an explicit override
Automatic choices should be easy to overrule. A file extension or query parameter such as /report.csv or ?format=csv lets a person get a particular version from an ordinary link, without setting headers. Let the explicit choice win, and keep it in the URL so it can be bookmarked and cached cleanly.
For APIs, document which formats exist, which one is the default and what happens when nothing matches. Clients then have no need to guess, and you can add a format later without surprising them.
Client hints and privacy
Detailed preference headers help servers, and they also help identify browsers. A rare combination of languages narrows down who you might be. Some browsers therefore shorten Accept-Language.
Client hints are an alternative in which a server asks for specific details, such as viewport width or platform, and the browser decides whether to send them. Support varies between browsers, so treat any hint as optional.
Testing your setup
From macOS or Linux, or with curl.exe on Windows, ask for different things and compare:
curl -sI -H "Accept-Language: es" https://www.example.com/
curl -sI -H "Accept-Encoding: br" https://www.example.com/app.js
curl -sI -H "Accept: image/avif" https://www.example.com/photo.jpg Check four things in each answer:
- The status is
200, or an intended redirect. -
Content-Type,Content-LanguageorContent-Encodingmatch what you expected. -
Varynames the headers that mattered. - Repeating the request with a different preference returns a different variant, including through your CDN.
What can go wrong
Wrong version for some visitors. Usually a missing Vary, or a cache that ignores it.
Garbled downloads. A file was compressed twice, or labeled with an encoding it does not have.
Downloads in place of pages. The server chose a data format because the client sent */*. Order your defaults so ambiguity yields the most common format.
Hard-to-share links. If one URL shows different things to different people, a shared link may not show what the sender saw. When the difference is meaningful to users, as with language, give each version its own address and keep negotiation as the helpful first guess.
For the wider context of these headers, see HTTP headers every site owner should know.