Search code examples
resthttpcontent-negotiation

HTTP header to return server available content types


E.g. Suppose we have a restful API endpoint to return orders, that can output in different formats.

[GET] /orders/42 

that could return xml, json or pdf.

I thought that the best option, in a restful paradigm, could have been to return available types as an header in the relative OPTION call:

[OPTION] /tests

But I do not know if there exists something like a content negotiation server header to list available content types.

I suppose that it should look something like:

Available-Content-Types: application/xml,application/json,application/pdf

Does something similar exists?


Solution

  • Content Negotiation works the other way around.

    The client makes a request for the resource (typically using GET) and includes an Accept header with a list of acceptable Content-Types (using a quality value to describe the preference order).

    The server then determines which response to send based on that.

    So a client might send:

    Accept: application/json;q=1, application/xml;q=0.9, application/pdf;0.5, */*;q-0.1
    

    Then the server would determine what type of data to return. Assuming all formats were equally good in the server's eyes, it would return JSON because that had the highest quality value from the client.