☼
⌃
☰

Content Negotiation

Table of Contents

Content Negotiation

Overview

Content negotiation is the mechanism within the Hypertext Transfer Protocol (HTTP) that allows a server to serve different versions (or representations) of a resource from the same URI. This process enables a client, like a web browser, to specify what kind of response it prefers, ensuring it receives content in a format it can handle and that is best suited for the user. For example, this could mean requesting a webpage in a specific language.

How Content Negotiation Works

The process is primarily managed through HTTP headers exchanged between the client and the server. The client sends specific headers with its request, indicating its preferences for different content characteristics. The server then analyzes these headers to determine which representation of the resource is the best fit for the client.

There are two main types of negotiation:

  • Server-driven Negotiation: This is the most common method. The client sends its preference headers, and the server makes the decision about which version of the resource to return.
  • Agent-driven Negotiation: In this method, the server responds with a list of available resource representations, and the client (or the user agent) chooses which one to request. This is often seen with a 300 Multiple Choices redirect status code.

The core of server-driven negotiation relies on these key HTTP request headers:

  • Accept: Specifies the media types (MIME types) the client can handle, like text/html or image/jpeg.
  • Accept-Language: Indicates the preferred natural language (e.g., en-US for American English).
  • Accept-Encoding: Lists the compression methods the client supports, such as gzip or br.
  • User-Agent: This header identifies the client software, which can be used to serve content tailored for specific browsers or device types (e.g., a mobile version of a site).

Examples

Here are some practical examples of content negotiation in action:

  • Requesting a Specific Language: A browser can indicate a user's language preference. If a user prefers French but will accept British English, the header might look like this: Accept-Language: fr-FR, en-GB;q=0.9 The server will first try to serve a French version. If that's not available, it will provide the British English version.
  • Requesting a Modern Image Format: A modern browser might prefer an efficient image format like WebP but can also handle older formats. The client sends: Accept: image/webp, image/apng, image/jpeg, */*;q=0.8 The server, if it has a WebP version of the image, will send it. Otherwise, it will fall back to another supported format like JPEG.
  • Serving Mobile vs. Desktop Sites: A server inspects the User-Agent string.
    • If the request is from Mozilla/5.0 (iPhone; CPU iPhone OS 17_5 like Mac OS X) …, the server can identify it as an iPhone and serve a mobile-optimized HTML and CSS file.
    • If the request is from Mozilla/5.0 (Windows NT 10.0; Win64; x64) …, it serves the full desktop version of the site.