Content Negotiation
What Is Content Negotiation in HTTP?
Content negotiation is an HTTP mechanism that lets a client and server select the best representation of the same resource, such as JSON, HTML, a language variant, or a compressed file. One URL can serve different valid forms without separate endpoints.
For example, an account endpoint may return application/json to an app and text/html to a browser. HTTP content negotiation also supports language and compression preferences.
How Content Negotiation Works
HTTP request headers describe a client's capabilities or preferences.
- The client sends preference headers, such as Accept: application/json.
- The server compares them with its supported representations.
- The server selects a compatible response or returns an appropriate error.
- The response identifies its format with Content-Type, such as application/json.
- If request headers affected the choice, the server sends Vary so caches keep variants separate.
For example, Accept: application/json;q=1.0, application/xml;q=0.5 means JSON is preferred. The q value is a preference weight, not a guarantee of server support.
Accept vs. Content-Type and Other HTTP Headers
Accept asks for a response format. Content-Type describes the format being sent.
| Header | Purpose |
|---|---|
| Accept | Lists response media types the client can use. |
| Content-Type | States the media type of a request or response body. |
| Accept-Language | Lists preferred languages, such as en-US. |
| Accept-Encoding | Lists supported compression methods, such as gzip or br. |
| Vary | Tells caches which request headers affected the response. |
Server-Driven, Agent-Driven, and Explicit Format Selection
Teams should choose and document one predictable approach.
| Approach | How it works |
|---|---|
| Server-driven | The server selects a match from Accept and related headers. |
| Agent-driven | The server may return 300 Multiple Choices or 406 Not Acceptable, letting the client choose or revise its request. |
| Explicit selection | A URL suffix or query parameter selects a format directly, such as?format=json. |
Benefits of Content Negotiation
Used carefully, negotiation keeps delivery flexible while resource logic stays consistent.
- Supports browser and programmatic clients from shared resource handling.
- Enables language variants and efficient compression.
- Keeps resource URLs stable as APIs evolve.
- Lets clients request representations they can process.
Practical Limits and Common Mistakes
Clients, servers, and caches must apply negotiation rules consistently.
- Ambiguous Accept headers need deterministic fallback behavior.
- Missing Vary can cause caches to serve the wrong variant.
- Accept is not an authorization or access-control mechanism.
- Media-type versioning can become difficult when many versions remain supported.
- If every client uses JSON, supporting only JSON is often simpler and more reliable.
Content Negotiation Best Practices for APIs
Document supported media types, defaults, and errors. Return accurate Content-Type and Vary headers, test caches, and use media types deliberately when versioning is necessary.
- Reject unsupported requested formats clearly, often with 406 Not Acceptable.
- Keep format selection separate from authentication and business rules.
- Test JSON, language, compression, and cache variants as real client scenarios.
When building and testing full-stack applications with AI-assisted app design and development, teams can specify API formats and verify response behavior early. Emergent helps turn requirements into API-ready applications, while its software glossary helps teams align on underlying terms.
on Emergent today


