Content Negotiation in WebAPI
Content negotiation is one of the great feature available in Web API. Client and Server do the content negotiation based on the type of the content.
ASP.NET WebAPI is built on top of REST. One of the main goal of modern applications is to deliver the resources in the format supported by the client. Now a days, web is not only connected by computers but also it is connected by smart phones, tabs, PDAs that is in one word we can say web is connected by Internet of Things (Anything which can connect to internet and transfer data is called an IoT device – smart TV, car whatever we can think). So, it may happen that one client supports content in one format and other client requires a different format. So, it is really required for the applications to be flexible to deliver the content on client required format. Here content negotiations comes into place.
Let us assume, our API can deliver the resources in XML and JSON format and the default format is JSON. Now in Http Header, there is one property called “Accept”. Client specifies which format it wants the resource with the Accept Header. If client specifies XML in Accept Header, then server would delivers the content in XML format. If client does not specify anything in the accept header, then content is sent to default format that is JSON. Now thinks client expects the data to be delivered in image/png. Our API does not support the delivery of content in client requested format. Then server will deliver in default format, which is JSON here. This entire process is called content negotiation.
Consider an example,
Let’s create a Web API project using Visual Studio Scaffolding. The default API controller is ValuesController.
We will use CURL to send the request to the API server. For details about CURL installation and usage, visit the below link,
ValuesController looks like below,
If we open a command prompt and do a request to the Values API, then we will get the below response.
We have received value1 and value2 what we are expecting.
Let’s put a debugger on the Get action and send a GET request (default) to Values controller once again,
Now if we do Shift + F9 which will open Quick Watch and type GlobalConfiguration.Configuration –
We see there are 4 formatter registered with Web API by default –
The default formatter is JsonMedia. If we notice the request sent to API, we find that we have not used any Accept Header. So, API responded with default formatter.
We can see this by issuing the same request with verbose flag
Notice the “Accept” header which is “*/*”.
Now try to get the XML content by modifying the Accept Header at the time of sending the request –
If we do the same request with –v flag, then we can see the Accept Header as,
So, our API is smart enough to send the request as per client understandable format.
Now, lets issue a request with Accept header as “image/png”,
We see that API returns data in Json array format because it does not support delivering the resources in requested format. So, it has delivered the content in default format which JsonArray.
Hope reader is having a basic understanding about the Content negotiations in Web API.