{"canonical_url":"https://engineer.company/glossary/content-negotiation/","content_markdown":"# Content negotiation\n\n## What it is\n\nHTTP content negotiation lets a service choose a representation for a request\nusing information supplied by the client or another defined selection mechanism.\nA client can send an Accept header that says which media types it can use and\nhow it prefers them. The server can select a supported representation and\ndescribe it with Content-Type. Other request fields can convey preferences about\nlanguage or encoding.\n\nThe information concerns one requested resource and the representations\navailable for it. A document can have an HTML representation for browsing and a\nJSON representation used by a program. A client that knows which format it needs\ncan express that preference. A server needs a rule for selecting from the\ncombinations it actually offers and for responding when none of them suit the\nrequest.\n\nQuality values can express relative preference, with zero marking an\nunacceptable choice under that request. Selection rules also have to respect the\nappropriate type matches and the server's supported representations. A route can\noffer a predictable default for broad or absent preferences. Give the default a\ndocumented meaning so different clients can interpret what they received.\n\n## Why it matters\n\nA shared resource can serve readers with different needs. A dashboard may want a\nstructured version of a document, while an ordinary browser wants a readable\npage. Returning an unexpected representation can lead to a confusing parser\nfailure or an unusable interface. A stable selection rule lets clients discover\nor express what they can handle and verify the resulting media type.\n\nSelection affects caching. When a response varies with Accept or another request\nheader, a shared cache needs that distinction to select the stored version. Vary\nis the response field that lets it account for the request fields used in\nselection. Without an appropriate distinction, the first stored representation\ncan be reused for later requests whose preferences differ. Test cache behaviour\nwith more than one representation of the same address.\n\nThere are also reasons for offering explicit URLs. A document link can point\ndirectly to its PDF, and a user can copy a stable Markdown address for a text\ntool. Where the choice is explicit in the URL, it can be easier to share and\ndiagnose. A site can use explicit choices for predictable user paths and still\nnegotiate specific resources when that helps its clients.\n\n## What it looks like in practice\n\nImagine a document endpoint with HTML and JSON representations. Send requests\nthat clearly prefer each type and check both the response headers and actual\nbody. Send a broad preference and determine which representation the documented\ndefault selects. Also test a request that accepts only an unavailable type. A\nservice can define how it handles that case, and a client should be able to\nrecognise the result.\n\nRepeat the exercise through the public delivery path with a cache enabled. Load\none representation first and then request the other at the same URL. Compare the\nstored and received headers and ensure the body matches the selected media type.\nRepeat in the opposite order. A test run directly against an origin can prove\nselection there. The full path proves whether a real shared cache honours the\nsame boundary.\n\nEngineer's [site credits and format documentation](/credits/) describes\nadditional ways the site makes its content available, including generated PDFs\nand feeds. Its user-facing format choices are examples of making the desired\nrepresentation clear. A service that uses negotiation should be equally explicit\nabout what it offers and how a client confirms the selected result. The\nillustrative endpoint gives a concrete way to test that rule.\n\nInclude conditional retrieval in a rollout fixture. Store one representation's\nvalidator, change the client's preference and ask again. The response should\nfollow the selected representation and its validation rules. This test checks\nthe interaction of preference, cache selection and identity, which may otherwise\nbe tested separately and leave their combination unexamined.\n\n## Where it goes wrong\n\nA request parser can treat all accepted values as equivalent. The service can\nthen return a format with a lower preference or mistake a broad wildcard for the\nclient's most specific choice. Use a tested parser appropriate to the protocol.\nAdd fixtures for multiple preferences, an explicit zero weight and a wildcard.\nCheck which type wins and record the reason.\n\nA second mistake is adding every potentially relevant request header to the\ncache's variation key. That can split reusable responses into many copies.\nIdentify which request fields actually participate and keep the policy aligned\nwith implementation. When a preference affects private content, review how the\nroute is stored in addition to how the variant is selected.\n\nLanguage preferences deserve a predictable override where they matter to\nreaders. A browser's preferred language may reflect a default installation or\nthe device owner. A reader opening a companion language link has made a more\ndirect choice. Define the precedence between a selected URL, a stored preference\nand the request header. Use Content-Language where it helps identify the\nresponse. Exercise the links in a fresh browser and after a manual change, and\nverify what happens on a shared or bookmarked URL.\n\nFinally, verify the body as a client will interpret it. A status code alone\nleaves the client's job uncertain. Check the media type, format, and signals\nthat help a reader choose an alternative when appropriate. Design the default so\na user can read or recover a useful result without knowing how a service weighs\nevery available preference.\n\n## Further reading\n\n- [RFC 9110 content negotiation](https://www.rfc-editor.org/rfc/rfc9110.html#section-12).\n  Defines representation selection and preference fields.\n- [RFC 9111 selecting stored responses by Vary](https://www.rfc-editor.org/rfc/rfc9111.html#section-4.1).\n  Explains how caches match request fields against stored responses.\n- [Mozilla's HTTP content negotiation guide](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Content_negotiation).\n  Provides examples of common forms and their headers.\n\n\u003chttps://engineer.company/glossary/content-negotiation/\u003e\n","date_modified":"2026-11-25T07:18:46Z","description":"Choose HTTP response media from supported representations and client preferences, then check how caches see the variation.","formats":{"gemtext":"https://engineer.company/glossary/content-negotiation/index.gmi","gopher":"https://engineer.company/glossary/content-negotiation/gophermap","html":"https://engineer.company/glossary/content-negotiation/","json":"https://engineer.company/glossary/content-negotiation/index.json","markdown":"https://engineer.company/glossary/content-negotiation/index.md","pdf":"https://engineer.company/glossary/content-negotiation/index.pdf"},"kind":"page","language":"en","title":"What is content negotiation?","url":"https://engineer.company/glossary/content-negotiation/","version":1}