Content Negotiation in HTTP
Client such as web browser requests a resource (such as HTML document)at a specific URL.In response ,server returns the resource at the requested URL to the client.When the server returns the resource at the requested URL ,it choses a specific representation of the resource such as HTML.
Different client may need different representations of the same resource.For example a client such as web browser may need HTML representation while another client such as a mobile device may require JSON.
The mechanism that decides which representation of the resource should be returned to the client is called Content Negotiation.
How Content Negotiation works
HTTP headers have an important part in Content Negotiation.
Content Negotiation works through the HTTP headers defined by the HTTP protocol.When a client makes a request for a resource ,it indicates which representation or MIME type it requires by sending the Accept HTTP header in the request.Client can also set the priorities of these MIME type.This means if one representation is not available then next representation is returned by the server based on the priority.
Accept http header
For example to indicate that HTML representation is required ,a client can set the Accept header as:
For XML representation client will set the accept header as:
Content-Type http header
The server responds with the requested representation and sets the Content-Type header. Content-Type denotes the MIME type of the returned representation of the requested resource.For example if the server is returning JSON then it will set Content-Type header as:
This will indicate to the client that the returned response is JSON and will render it accordingly.