REST API - Content Negotiation

REST API - Content Negotiation

Please find part 1 of this series in the above link.

Generally, REST resources can have multiple representations of the data. Different clients may expect different representations.

Asking for a specific format of the resource via the REST URL is called content negotiation.

The process of selecting the best representation for a given response when there are multiple representations available is called Content Negotiation.

This identification and negotiation can be done in two ways:

  1. Using specific HTTP headers - Server-driven content negotiation or proactive negotiation
  2. Selecting a compatible resource from the list of available resources - (agent-driven negotiation?or?reactive negotiation)

Server-driven Content Negotiation

This is achieved by sending several HTTP headers with the URL with which the server can format the response. If it can't provide a suitable resource, it might respond with?406?(Not Acceptable) or?415?(Unsupported Media Type) and /or set headers for the types of media that it does support (using the?Accept-Post for POST and?Accept-Patch?for PATCH requests).

No alt text provided for this image
Server-driven content negotiation

The different HTTP headers available are:

  1. Accept: Used to tell the server what media types can be sent
  2. Accept-Language: Used to tell the server what languages can be sent
  3. Accept-Charset: Used to tell the server what charsets can be sent
  4. Accept-Encoding: Used to tell the server what encodings can be sent

Although flexible, server-driven content negotiation has a few drawbacks. It does not scale well. Sending every request with precise headers/custom headers to get the appropriate response from the server may cause an increase in the message size. This could affect the performance of the server.

Agent-driven Content Negotiation

To allow the client to choose from the list of resources available on the server, agent-driven content negotiation was built.

But, this will need multiple requests to get the appropriate response from the server; the server will return the list of resources with a 300 header, from which the client then chooses the actual resource from the list.

No alt text provided for this image
Agent-driven content negotiation

Since there is no specific HTTP format on how to select the required format from the available list of resources, this process cannot be automated. Two requests to get the resource really slow down the system. And the second request, to actually request the specific format, falls back to server-driven content negotiation, which prevents developers from using this type of content negotiation.

Implementation of server-driven content negotiation is made easy by Spring Boot's starter:

<dependency
			<groupId>com.fasterxml.jackson.dataformat</groupId>
			<artifactId>jackson-dataformat-xml</artifactId>
			<version>2.15.0-rc3</version>
</dependency>        

Based on the Accept header, Jackson will perform the required data formatting.

With value application/json

No alt text provided for this image

With value application/xml

No alt text provided for this image

References:

  1. https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation
  2. https://www.holisticseo.digital/technical-seo/http-header/content-negotiation/

Harshit Anand

Software Developer @ Delta Airlines

1 å¹´

Insightful ??

要查看或添加评论,请登录

Preethi Pattabiraman的更多文章

  • Switch-Case

    Switch-Case

    There is nothing that helps a developer like a switch case statement. With multiple conditions and business logic to…

    2 条评论
  • MongoDB Operators

    MongoDB Operators

    MongoDB provides a wide range of intuitive operators that help us to query the collections for the relevant documents…

    1 条评论
  • Java Integer Cache

    Java Integer Cache

    I was astounded to learn about this feature in Java called the IntegerCache. A new functionality was added to reduce…

  • Circuit Breaker

    Circuit Breaker

    A microservice always needs its friends, the supporting microservices. They all collaborate to make an application…

    1 条评论
  • REST API - Documentation

    REST API - Documentation

    There are some basic REST API features that are expected from any API, such as Documentation Content Negotiation I18N…

    9 条评论
  • Finally

    Finally

    Many of the interview questions are aimed to make us pause. But, if you note those little pointers, it will be a…

社区洞察

其他会员也浏览了