Content negotiation

What it is

HTTP content negotiation lets a service choose a representation for
a request using information supplied by the client or another
defined selection mechanism. A client can send an Accept header that
says which media types it can use and how it prefers them. The
server can select a supported representation and describe it with
Content-Type. Other request fields can convey preferences about
language or encoding.

The information concerns one requested resource and the
representations available for it. A document can have an HTML
representation for browsing and a JSON representation used by a
program. A client that knows which format it needs can express that
preference. A server needs a rule for selecting from the
combinations it actually offers and for responding when none of them
suit the request.

Quality values can express relative preference, with zero marking an
unacceptable choice under that request. Selection rules also have to
respect the appropriate type matches and the server's supported
representations. A route can offer a predictable default for broad
or absent preferences. Give the default a documented meaning so
different clients can interpret what they received.

Why it matters

A shared resource can serve readers with different needs. A
dashboard may want a structured version of a document, while an
ordinary browser wants a readable page. Returning an unexpected
representation can lead to a confusing parser failure or an unusable
interface. A stable selection rule lets clients discover or express
what they can handle and verify the resulting media type.

Selection affects caching. When a response varies with Accept or
another request header, a shared cache needs that distinction to
select the stored version. Vary is the response field that lets it
account for the request fields used in selection. Without an
appropriate distinction, the first stored representation can be
reused for later requests whose preferences differ. Test cache
behaviour with more than one representation of the same address.

There are also reasons for offering explicit URLs. A document link
can point directly to its PDF, and a user can copy a stable Markdown
address for a text tool. Where the choice is explicit in the URL, it
can be easier to share and diagnose. A site can use explicit choices
for predictable user paths and still negotiate specific resources
when that helps its clients.

What it looks like in practice

Imagine a document endpoint with HTML and JSON representations. Send
requests that clearly prefer each type and check both the response
headers and actual body. Send a broad preference and determine which
representation the documented default selects. Also test a request
that accepts only an unavailable type. A service can define how it
handles that case, and a client should be able to recognise the
result.

Repeat the exercise through the public delivery path with a cache
enabled. Load one representation first and then request the other at
the same URL. Compare the stored and received headers and ensure the
body matches the selected media type. Repeat in the opposite order.
A test run directly against an origin can prove selection there. The
full path proves whether a real shared cache honours the same
boundary.

Engineer's site credits and format documentation describes
additional ways the site makes its content available, including
generated PDFs and feeds. Its user-facing format choices are
examples of making the desired representation clear. A service that
uses negotiation should be equally explicit about what it offers and
how a client confirms the selected result. The illustrative endpoint
gives a concrete way to test that rule.

Include conditional retrieval in a rollout fixture. Store one
representation's validator, change the client's preference and ask
again. The response should follow the selected representation and
its validation rules. This test checks the interaction of
preference, cache selection and identity, which may otherwise be
tested separately and leave their combination unexamined.

Where it goes wrong

A request parser can treat all accepted values as equivalent. The
service can then return a format with a lower preference or mistake
a broad wildcard for the client's most specific choice. Use a tested
parser appropriate to the protocol. Add fixtures for multiple
preferences, an explicit zero weight and a wildcard. Check which
type wins and record the reason.

A second mistake is adding every potentially relevant request header
to the cache's variation key. That can split reusable responses into
many copies. Identify which request fields actually participate and
keep the policy aligned with implementation. When a preference
affects private content, review how the route is stored in addition
to how the variant is selected.

Language preferences deserve a predictable override where they
matter to readers. A browser's preferred language may reflect a
default installation or the device owner. A reader opening a
companion language link has made a more direct choice. Define the
precedence between a selected URL, a stored preference and the
request header. Use Content-Language where it helps identify the
response. Exercise the links in a fresh browser and after a manual
change, and verify what happens on a shared or bookmarked URL.

Finally, verify the body as a client will interpret it. A status
code alone leaves the client's job uncertain. Check the media type,
format, and signals that help a reader choose an alternative when
appropriate. Design the default so a user can read or recover a
useful result without knowing how a service weighs every available
preference.

Further reading

  * RFC 9110 content negotiation. Defines representation selection
and preference fields.

  * RFC 9111 selecting stored responses by Vary. Explains how caches
match request fields against stored responses.

  * Mozilla's HTTP content negotiation guide. Provides examples of
common forms and their headers.

hThis page on the web	URL:https://engineer.company/glossary/content-negotiation/	engineer.company	70
