Content Negotiation can be defined as the process of selecting best representation for a given resource. So Content negotiation means the client and server can negotiate between them so that client can get data according to their required format.
There are three points on which internet depends,
Third point is more important than other two, because everything is works on the basis of how we can see the resource. We can represent a resource in two formats.
One of the standards of the RESTful service is that, the client should have the ability to decide in which format they want the response either in JSON or XML. A request that is sent to the server includes an Accept header. Using the Accept header the client can specify the format for the response.
For example,
Accept: application/xml
returns result in XML format
Accept: application/json
returns result in JSON format
Depending on the Accept header value in the request, the server sends the response. This is called Content Negotiation.
What happens behind the scene when we request data in specific format?
The ASP.NET Web API controller generates the data that we want to send to the client and hands the data to the Web API pipeline which then look for Accept header of the client. Then, choose a appropriate formatter to format the data.
As ASP.NET Web API is greatly extensible, we can also specify multiple values for accept header in the request header.
Accept: application/xml,application/json
In the above case, server choose the first formatter to format the data of response.
We can also specify quality factor in the accept header. In this case, server choose a format which have higher quality factor.
Accept: application/json;q=0.8,application/xml;q=0.5
If we don't specify any Accept header, then by default server choose JSON formatter.
When the response is being sent to the client in the requested format, notice that the Content-Type
header of the response is set to the appropriate value. For example, if the client has requested application/xml
, the server send the data in XML format and also sets the Content-Type=application/xml
.
The formatters are used by the server for both request and response messages. When the client sends a request to the server, we set the Content-Type header to the appropriate value to let the server know the format of the data that we are sending.
For example, if the client is sending JSON data, the Content-Type header is set to application/json
. The server knows it is dealing with JSON data, so it uses JSON formatter to convert JSON data to .NET Type. Similarly when a response is being sent from the server to the client, depending on the Accept header value, the appropriate formatter is used to convert .NET type to JSON, XML etc.
Example of different types of response format:
application/json:
{
"Email": "sample string 1",
"HasRegistered": true,
"LoginProvider": "sample string 3"
}
application/xml:
<UserInfoViewModel xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/WebApiDemo.Models">
<Email>sample string 1</Email>
<HasRegistered>true</HasRegistered>
<LoginProvider>sample string 3</LoginProvider>
</UserInfoViewModel>
Modern web based applications can provide data in various languages and formats. So, if we develop our API to cover global users across the world, then Content Negotiation is relevant.