Skip to content

Content Negotiation

A single resource can have several representations — JSON, CSV, different languages. Content negotiation lets the client state a preference and the server pick the best match, all without changing the URI.

The client sends Accept (which media types it wants) and optionally Accept-Language; the server replies with the chosen representation and a matching Content-Type:

GET /reports/42 HTTP/1.1
Accept: text/csv, application/json;q=0.8
Accept-Language: th, en;q=0.5

The q values are quality weights — here the client prefers CSV, then JSON. The server should honor the best type it supports and echo it in Content-Type.

  • If the server cannot produce any accepted type, respond 406 Not Acceptable.
  • If a request body arrives in a type the server cannot parse, respond 415 Unsupported Media Type.
JavaScript
Which header does a client use to request a representation format?
The server supports none of the types in Accept. Which status?
A request body arrives in a media type the server cannot parse. Which status?