Negotiation of Content in.NET Core Web API

Content negotiation is the process of selecting the optimal resource for a response when many resource representations are available. For a variety of reasons, content negotiation may not be utilized as frequently as it may be despite being an HTTP function for some time.

In short, content negotiation lets you choose—or better yet, “negotiate”—the content that gets returned when you send out a REST API request.

Negotiation of content
What’s That?
One limitation of REST services is that the client needs to be able to choose the format (XML or JSON, for example) in which they receive their response. We call this negotiation of content.

More specifically, with REST services, a client and server can decide on the format of the data to be transferred by using a technique known as content negotiation. By following this process, it is ensured that the data format will be understandable and acceptable to all parties communicating.

How are Content Negotiations handled?

Accept and Content-Type are the two HTTP headers that are mainly involved in content negotiation.

  • Accept Header: The client uses this field to indicate the kinds of media that it is ready, willing, and able to handle. To specify whether a client wants to receive data in the XML (application/xml) or JSON (application/json) formats, for example, the client can use the Accept header.
  • Content-Type Header: The server uses the Content-Type Header to indicate the media type of the response content that it is sending back to the client. It guarantees that the client is capable of parsing and processing the received data by informing them of the format in which the response data is encoded.

ASP.NET Core Web API ignores the browser’s Accept header and returns a JSON-formatted result by default.

The following media types are supported by ASP.NET Core by default:

  • application/json
  • text/json
  • text/plain

ASP.NET Core Web API Content Negotiation Implementation

Because of its built-in infrastructure, ASP.NET Core Web API makes the implementation of content negotiation simpler. By configuring custom formatters, the framework can support additional formats in addition to JSON and XML, which are supported out of the box.

  • Built-in Support: JSON is the main data exchange format for ASP.NET Core Web API projects by default. Formatters in the Program.cs file can be configured to enable XML and other formats.
  • Custom Formatters: By extending the TextInputFormatter and TextOutputFormatter classes, or their binary equivalents for non-text formats, developers can create custom Formatters to support new content types.
  • Configuration: Developers can add or configure Formatters in the Program.cs Main method by using the AddMvc or AddControllers methods (for API projects). Here, one can set up custom formatters, configure options, and list supported media types.

The Default JSON Response

Let’s now send an HTTP GET Request to the endpoint, instructing it to send the response in JSON format, as seen in the image below, by using the Accept header and setting its value to application/json. Kindly adjust the port number that your application is currently using:

Let’s now send a second HTTP GET request to the aforementioned endpoint, setting the Accept header value to application/xml as seen in the image below, and observe the format of the response that we receive.

As seen in the image above, we receive the response in XML format even when we set the Accept header value to application/XML. This is a result of our application’s XML formatters not being enabled yet. JSON Formatter is the only formatter that is enabled by default, and it is this formatter that sends the response by default regardless of the value of the Accept header. The value of the Content-Type header is application/XML, even if you inspect the response header.

Return XML Response

Where a server formats a response to JSON is not made clear. However, we can override it by modifying the AddControllers() method’s configuration options. It is located in the Program class by default and has the following appearance:

When the client attempts to negotiate for the XML response, we can add the following options to allow the server to format it:

Initially, we need to instruct a server to honor the Accept header. The AddXmlSerializerFormatters() method is then added to support XML formatters.

After configuring our server, let’s try the content negotiation again.

Our XML response is shown there; it is no longer the default response. We can obtain responses in a different format by modifying the Accept header.

Restricting Media Type in Content Negotiation

As of right now, if the media type is not detected, JSON will be returned as the default response.

However, we can limit this behavior by including the following line in the configuration:

We’ve added the ReturnHttpNotAcceptable = true option, which instructs the server to return the 406 Not Acceptable status code in the event that the client attempts to negotiate for a media type that the server does not support.

As a result, the API consumer will be forced to request only those types that the server supports, increasing the restrictions in your application. For this reason, the 406 status code was developed.

Let’s now attempt to retrieve the text/CSV media type using Postman and observe the results.

As anticipated, the response body is absent, leaving us with nothing more than a polite 406 Not Acceptable status code.

We learned the new technique and evolved together.

Happy coding!

ASP.NET 8.0.4 Hosting Recommendation

ASP.NET is a powerful platform for creating web applications and services. You must be comfortable with JavaScript, HTML, CSS, and C# before developing a web application in ASP.NET. On the market, there are thousands of web hosting companies providing ASP.NET Hosting. But, only very few web hosting companies could provide high quality ASP.NET hosting solution.

ASP.NET is the best development language in Windows platform, which is released by Microsoft and widely used to build all types of dynamic Web sites and XML Web services. With this article, we’re going to help you to find the best ASP.NET Hosting solution in Europe based on reliability, features, price, performance and technical support. After we reviewed about 30+ ASP.NET hosting providers in Europe, our Best ASP.NET Hosting Award in Europe goes to HostForLIFE.eu, one of the fastest growing private companies and one of the most reliable hosting providers in Europe.

You may also like...

Popular Posts