liveBook · Manning

Content negotiation

As explained in The Design of Web APIs, Second Edition

HTTP content negotiation is the HTTP mechanism by which a client and server select an appropriate representation format or language for a message.

Content negotiation lets an HTTP client and server select a suitable representation format or language for a message. Clients request alternatives with headers such as Accept, Content-Type, and Accept-Language; servers identify the result with Content-Type. The technique helps different consumers use the same API without forcing one data format or locale. section 9.7.1 section 9.7.2

Apply content negotiation where the variation solves a real consumer need:

  • Offer JSON by default while allowing alternatives such as CSV for spreadsheet users or XML for consumers with native XML tooling. section 9.7.1
  • Keep request and response formats independent: a consumer can send JSON and request an XML response, or reverse the combination. section 9.7.1
  • Translate human-facing values with Accept-Language, including language priorities such as fr;q=0.9, while leaving property names and machine-readable codes unchanged. section 9.7.2
  • Apply formats consistently across comparable operations, but support only formats that make sense for each operation. section 9.10.3 section 9.7.1
  • Use custom media types or the Prefer header when consumers need different resource models, but recognize that both choices are uncommon and may surprise developers. section 13.5.1

Transactions in the consumer’s preferred form

A spreadsheet user requests GET /accounts/12345/transactions with Accept: text/csv. The API returns CSV with Content-Type: text/csv. A consumer that omits Accept receives the default JSON representation. A banking client can instead POST /transfers with Content-Type: application/xml and request an XML or JSON response independently. section 9.7.1

Selecting a resource model

GET /accounts can return a summarized model when the client sends Accept: application/vnd.bankingapi.summarized+json, or a complete model when it sends Prefer: model=complete. The response identifies the selected representation with Content-Type or Preference-Applied. section 13.5.1

Figure 9.11 Content negotiation allows consumers to request an account’s transactions in different formats by sending a standard Accept header.
figure

Use the figure to compare a standard Accept request with the negotiated transaction response. section 9.7.1

Figure 13.9 We can use a custom media type or preference to get the complete or summarized model on any read, list, or search operation.
figure

Use the figure to compare custom media types and Prefer for summarized or complete resource models. section 13.5.1

How The Design of Web APIs, Second Edition covers it

How deep it goes
Explains headers, defaults, independent request and response formats, consistency, and trade-offs without specifying exhaustive media-type handling.
Examples
Demonstrates CSV transactions, JSON and XML transfers, localized labels, hypermedia formats, and summarized or complete account models.
What it assumes
Assumes familiarity with HTTP requests, responses, headers, and media types while treating defaults and consumer needs explicitly.
What you can do after
Apply it via HTTP headers to design flexible representations while preserving clear defaults, consumer interoperability, and operation-level consistency.
How it connects
Connects HTTP headers, media types, localization, hypermedia APIs, resource models, file retrieval, and API evolution.

Also explained in

Compare how 3 books explain content negotiation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Design of Web APIs, Second Edition. The section links point to the parts of the book it draws on.