HTTP - Content Negotiation



Content negotiation is a mechanism defined in the HTTP protocol that makes it possible to serve different versions of a resource at the same URI, so that user agents can specify which version fits their capabilities the best.

Content negotiation example

Content Negotiation Methods

Here, we have mentioned two content negotiation methods:

Server-driven negotiation

The server selects the best representation based on the request header sent by client. The server sends a list of available representations to the user agent, and the user agent selects the best representation based on its preferences. These request headers include following headers:

  • Accept
  • Accept-Charset
  • Accept-Encoding
  • Accept-Language

Accept Header

It is used to specify MIME types that client can handle such as text/html, application/json. It is a comma-separated list of MIME types combined with a quality factor q. Here q represents relative preferences of MIME types when multiple MIME type is specified.

Example

In this example, we have specified two MIME types i.e application/json and text/html with q value as 0.8. It returns text/html as server does not support application/json.

GET /index.htm HTTP/1.1
Host: tutorialspoint.com
Accept: application/json, text/html;q=0.8

Output

Accept header Output

Accept-Charset Header

The header Accept-Charset is used to specify acceptable character encoding that clients can process or understand. For example: UTF-8 or ISO-8859-1.

Example

GET /index.htm HTTP/1.1
Host: tutorialspoint.com
Accept-Charset: UTF-8

Output

Accept-Charset Output

Accept-Encoding Header

The header Accept-Encoding is used to specify the encoding that client can process or understand. For example: gzip, deflate, br.

Syntax

Accept-Encoding: gzip | deflate | br | compress | zstd | identity | *

Example

GET /index.htm HTTP/1.1
Host: tutorialspoint.com
Accept-Encoding: gzip

Output

Accept-Encoding Output

Accept-Language Header

The header Accept-Language is used to specify the client's preferred language that client can understand. For example: en, en-US, en-GB. The server sends 406 Not Acceptable error code if it is unable to serve content in the matching language specified by this header.

Example

GET /index.htm HTTP/1.1
Host: tutorialspoint.com
Accept-Language: en-US

Agent-driven negotiation

This method is client dependent negotiation where client selects from available resources. In this method server presents all the alternate available resources to client and client decides which choice to select. The status codes for agent-driven negotiation are mentioned below:

  • 300 Multiple Choices
  • 406 Not Acceptable

300 Multiple Choices

If the resource requested by any client has multiple representations, then server responds with 300 multiple choices status code and sends all the representations of the requested resource. The client then selects one option from the list of all available options.

Example

HTTP/1.1 300 Multiple Choices
Content-Type: text/html
<ul>
  <li><a href="/resource.json">JSON</a></li>
  <li><a href="/resource.html">HTML</a></li>
</ul>

406 Not Acceptable

The server returns status code 406 Not Acceptable when server can not find resource with client's matching preferences.

Example

HTTP/1.1 406 Not Acceptable
Content-Type: text/plain

No acceptable representation found for the requested resource.

Server Driven v/s Agent Driven Negotiation

Server driven and agent driven negotiation both have their use cases, and can be used according to application's requirements but there are many differences among them even though they are used for the same purpose i.e content negotiation.

Server-driven Agent-driven
Server selects the best representation based on the request header sent by client. Client selects from available resources based on server response.
It has limited visibility for the client. Client full visibility and control.
It is used in simple scenarios. It is used in complex cases that requires client side decision.
It uses headers such as Accept, Accept-Charset, Accept-Language etc. It uses response status codes such as 300 Multiple Choices and 406 Not Acceptable.
Advertisements