ASP.NET Web API Content Negotiation

Overview - In this article, we will see Web API Content negotiation with an example. So, let’s start.

Introduction - So what is ASP.NET Web API Content Negotiation?

One of the standards of the RESTful services is that the client should be able to decide the format of response from the server. For example, if the client wants the response in XML or in JSON and so on.

When a client sends a request to the server, the request includes something called ACCEPTHEADER. Using this acceptheader the client can specify the format which they want from the server.

For example - If the accept header is XML, we write as, Acceptpt:application/xml the service sends the format in XML. If it is JSON, then Acceptpt:application/json the service sends the format in JSON.

Now, just type in the URL .



You will get this output. Now, kindly open Fiddler to see the content type for this URL.

In Fiddler, click on XML tab.



We have got the data in XML format as the list of employees. Now, let’s click on headers and see the content negotiation.



When you click on header, the content-type is application/xml that means the client has requested output in XML format.

Now, if you want it to be in JSON.



Write  Accept: application/json and click on execute. When clicked on JSON tab, you will see.



Now, click on header. We should see Content-Type: application/json, as shown below.



Depending upon the accepted header value in the request, the server sends the response. This is called as Content Negotiation.

Now, we will understand what a web API does when we request for a specific data. Remember, the Web API generates the Controller data that we want to set to the client.

In our example, the employees’ Controller,



We are generating the employees list which we want to return to the client. Once the list of the employees is generated, the job of the Controller is over. It’s going to hand that list to the Web API pipeline which then, looks at the accept header and depending upon the format which client has requested, Web API will choose the appropriate formatter for formatting the data.

For Example: If the client has requested for XML Data, Web API uses XML formatter; if the client has requested for JSON Data, Web API uses JSON formatter. These formatters are nothing but classes and they are called media typed formatters.

We know that ASP.NET Web API is extensible; this means, we can also plugin our own formatters for custom formatting our data. We can also specify multiple values for the accept header.

In the above screen shots we have accept header as XML and JSON. Now the server will look at the list of all formatters available and then it’s going to pick the first formatter which happens to be the JSON formatter which is going to format the data in JSON.

You can also specify the quality factor as,

Application/xml; q=0.8, application/json; q=0.5. Here XML quality factor is 0.8 and json quality factor is 0.5.

In this case XML has got higher quality factor. Now let’s go back to our fiddler and let’s set the quality factor as,



Here, we have set the quality factor for JSON and XML and now, execute that. You will get the data in XML, as shown below,



Because the XML quality factor we had mentioned is higher than JSON, we have got the output in XML.

Now, if don’t set any Acceptheader, then by default Web API returns JSON data.

Now, let’s remove the accept header and execute the request and see what output we get by default,



Execute it and you will see,



The default output is JSON format.

NOTE - One important thing is that these media typed formatters are used by the servers for both request and response messages. When a 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 value that we are sending.

For example - If the client is sending data in JSON the application/json is set. So in this case the server knows its dealing with JSON Data. So its uses JSON formatter to convert JSON data to the content type.

First we will look at the JSON data and how is to be intended,



We will request JSON Data and execute it.

Now click on the Raw tab you will get JSON data which is not properly intended.



When you look at the data you will PascalCase we want data to be CamelCase.So in our WebApiconfig .cs we add two lines of code as to serialize JSON output and we want camelcase letters as,



Now build the solution and go to fiddler,



Here we are requesting JSON data execute that request. Click on Raw tab and you will notice,



As the JSON data is properly intended and the property names are using camel case instead of Pascal Case.

Conclusion - This was all about ASP.NET Web API Content Negotiation. Hope this article was helpful!!


Similar Articles