Amul Amul - 1 month ago 22
HTTP Question

Content Negotiation in Web API via Agent Driven Negotiation (HTTP)

I understand how Server Driven Negotiation takes place via setting Accept and Content-Types. But am confused how is Agent Driven Negotiation taking place.

Documentation to refer - https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation

The document says Agent Driven Negotiation involves server returning all possible options for a web resource, based on the URL. But since the file format of the response is not present, how is the user-agent able to determine which file format to pick. Also why do we need a second API call in this case to fetch a resource?

Answer

The formats that the server supports are sent in the body of the first response (2 in the image). The server is free in choosing a representation of his choice to encode the different options, e.g. a JSON array of objects describing each possible choice.

enter image description here

The reason why this scheme is not often used in Web/REST APIs is that there is no standard for the 300 Multiple Choices response. Also, the second round-trip has a negative impact on performance.