← Lessons

quiz vs the machine

Gold1360

Networking

Content Negotiation Deep Dive

How client and server agree on format, language, and encoding.

5 min read · core · beat Gold to climb

Choosing the Best Representation

A single URL may have several representations: different formats, languages, or compressions. Content negotiation lets the client express preferences and the server pick the best match.

Proactive Negotiation

In proactive negotiation the client sends Accept headers and the server decides.

  • Accept lists media types like text html or application json.
  • Accept Language lists preferred human languages.
  • Accept Encoding lists compressions like gzip or br.

Each value may carry a quality value, written as q, between 0 and 1 to rank preferences.

Reactive Negotiation and Vary

In reactive negotiation the server returns a list of choices and the client picks. Either way, when a response depends on request headers, the server must send a Vary header naming those headers so caches store each variant separately.

Why Vary Matters

Without Vary, a cache could serve a French page to an English client or compressed bytes to a client that cannot decompress them.

Key idea

Content negotiation matches one URL to the best representation using Accept headers and quality values, and the Vary header tells caches which request headers changed the answer.

Check yourself

Answer to earn rating on the learn ladder.

1. What does a quality value in an Accept header express?

2. Why must a server send a Vary header for negotiated responses?