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.
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: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:Fetch the resource and send an
Accept
header containing*/*
, implying every imaginable media type were understood by the client.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, */*
Send out a new request for the same resource with the new accept header created in step 2
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.