One address on a server can send the same data in several forms: JSON for an app, HTML for a browser. So how does the server know which form was asked for?
The client (the browser or app that sends the request) names the formats it prefers in a header. Then the server picks the best match from the formats it has. This small bit of bargaining is called content negotiation.
The client asks, the server picks
In this example, the client asks for the data of user number 42. The first block is the request, the second is the server's reply:
GET /users/42 HTTP/1.1
Accept: text/html;q=0.5, application/json
HTTP/1.1 200 OK
Content-Type: application/json
Vary: Accept
{"id": 42, "name": "Budi"}
Acceptis a request header: the list of formats the client wants back, separated by commas.text/htmlandapplication/jsonare MIME types: names of formats, written astype/subtype.;q=0.5is how much the client likestext/html.application/jsonhas noq, so its value is1.Content-Type: application/jsonis in the server's reply. It means "what I'm sending is JSON".Vary: Acceptis explained later, in the section about Vary.
The result: JSON is worth 1 and HTML 0.5, so the server sends JSON.
The q value: how much it likes it
q is a number from 0 to 1. The bigger the number, the more the client likes that format. Without a q, the value is 1.
- The
qvalue decides, not the order the formats are written in.text/plain;q=0.3, text/html;q=0.9, application/jsonmeans JSON first, then HTML, then plain text. q=0means refuse: "don't send this format". Sotext/html;q=0isn't "likes HTML a little", it's "no HTML".- The server only picks from the formats it has, and it takes the one with the highest value. When two values are equal, the server uses its own order of preference.
- If no format matches, the server may reply
406 Not Acceptable. It may also simply send its default format.
The star: any format
*/* matches any format. text/* matches every format of type text, such as text/html and text/plain. But text/* does not match application/json. A browser usually sends a header like text/html, */*;q=0.8, which means "I like HTML best, other formats are fine too".
Language and compression are negotiated too
Each negotiation header in the request has a partner header in the server's reply:
| The client asks with | Example | The server answers with |
|---|---|---|
Accept |
application/json |
Content-Type |
Accept-Language |
fr, en;q=0.5 |
Content-Language |
Accept-Encoding |
gzip, br |
Content-Encoding |
In the Accept-Language row, fr is the code for French and en for English. Accept-Encoding lists the kinds of compression (squeezing data smaller) that the browser can unpack. The q values in these three headers follow the same rules.
Vary: so caches don't mix things up
A cache keeps saved copies of replies, in the browser or on a CDN. Without Vary, a cache only looks at the URL. So a JSON reply for /users/42 can be given to a browser that asked for HTML at the same URL. Vary: Accept means "keep a separate copy for each value of Accept". For languages, write Vary: Accept-Language.
⚠️ Swapped headers and the more specific line
- Mixing up
AcceptandContent-Type.Acceptis in the request: the formats the client wants back.Content-Typenames the format of the body being sent. - The more specific line comes first. A line here means one part of the list in
Accept, such astext/html;q=0. In real HTTP, one format can match several lines, and the most specific line counts. For example, intext/html;q=0, */*, HTML matches both lines, buttext/html;q=0is more specific. So this list means "anything is fine, except HTML". In the exercise, you only need to take the highestq, because its examples never clash with this rule.
TL;DR: The client names the format it wants in the Accept header, the language in Accept-Language, and compression in Accept-Encoding. Each choice can have a q value from 0 to 1, which says how much it is liked: no q means 1, q=0 means refuse. The server picks the highest-valued format it has and names it in Content-Type, or replies 406 when nothing matches. */* matches any format, and text/* every text format. Vary makes caches keep a separate copy for each choice.
⚡ The client asks, the server picks
Write:
GET /users/42 HTTP/1.1
Accept: text/html;q=0.5, application/json
Guess first: q=0.5 says how much the client likes HTML, and a format with no q is worth 1. The server has HTML and JSON. Which one does it send?
JSON. application/json has no q, so its value is 1, while HTML is only 0.5.
Meaning: Accept is a header that tells the server which formats the client wants back. text/html and application/json are names of formats, called MIME types. The server picks the best match from the formats it has, then names it in the Content-Type header of its reply.
⚡ The q value: how much it likes it
Guess first: does text/html;q=0 mean it likes HTML a little?
No. q=0 means refuse: don't send HTML.
Meaning: q is a number from 0 to 1: the bigger it is, the more the format is liked. No q means 1. The q value decides, not the order the formats are written in. If no format matches, the server may reply 406 Not Acceptable.
⚡ Language, compression and caches
Meaning: The language is chosen the same way as the format: the client names it in Accept-Language, and the server answers in Content-Language. Compression (squeezing data smaller) is named in Accept-Encoding and answered in Content-Encoding. A Vary: Accept header in the reply makes caches keep a separate copy for each value of Accept. Without this header, a cache only looks at the URL, so a JSON reply could be given to a browser that asked for HTML.