How to fetch every possible media types

387 views Asked by At

Having a resource identified by the URL https://example.com/resource, accepting image/svg+xml and text/html media types (using HTTP's Accept header).

How can a client be aware of those possible media types? We frequently refer to Accept header as a negotiation between the client and the server. But I don't really understand how the client can give to the server its wanted type, and how the server is supposed to answer.

I think the server may be able to teach to the client its allowed types.

1

There are 1 answers

1
DaSourcerer On BEST ANSWER

I think this needs to be split up:

If you are a server and wish to inform clients about alternative representations of the requested resource, you can do so via the Link header as described in RFC 5988. A possible response could look like this:

HTTP/1.1 200 Ok
Content-Type: image/svg+xml
Link: <https://example.com/resource>; rel="alternate"; type="image/png"
Link: <https://example.com/resource>; rel="alternate"; type="image/jpeg"

If a client fetches a resource and wants to know which alternative representations are available, this were the way to go if no Link headers can be found:

  1. Fetch the resource and send an Accept header containing */*, implying every imaginable media type were understood by the client.

  2. Extract the media type from the response and add it to your Accept header with a weight q=0, letting the server know the served media type were not acceptable. Example:

    Accept: image/svg+xml;q=0, */*

  3. Send out a new request for the same resource with the new accept header created in step 2

  4. If you receive a 406/Not acceptable response, you're done. If not: Go back to step 2.

You should now know all media types for the given resource on the server.