I'm looking into a restful design and would like to use the HTTP methods (POST
, GET
, …) and HTTP headers as much as possible. I already found out that the HTTP methods PUT
and DELETE
are not supported from the browser.
Now I'm looking to get different representations of the same resource and would like to do this by changing the Accept
header of the request. Depending on this Accept
header, the server can serve a different view on the same resource.
Problem is that I didn't find a way to tell my browser to change this header.
The <a..>
tag has a type attribute, that can have a mime type, looked like a good candidate but the header was still the browser default (in Firefox it can be changed in about:config
with the network.http.accept.default
key).
Best Answer
I would partially disagree with Milan's suggestion of embedding the requested representation in the URI.
If anyhow possible, URIs should only be used for addressing resources and not for tunneling HTTP methods/verbs. Eventually, specific business action (edit, lock, etc.) could be embedded in the URI if create (POST) or update (PUT) alone do not serve the purpose:
In the case of requesting a particular representation in URI you would need to disrupt your URI design eventually making it uglier, mixing two distinct REST concepts in the same place (i.e. URI) and making it harder to generically process requests on the server-side. What Milan is suggesting and many are doing the same, incl. Flickr, is exactly this.
Instead, a more RESTful approach would be using a separate place to encode preferred representation by using
Accept
HTTP header which is used for content negotiation where client tells to the server which content types it can handle/process and server tries to fulfill client's request. This approach is a part of HTTP 1.1 standard, software compliant and supported by web browsers as well.Compare this:
to this:
From a web browser you can request any content type by using
setRequestHeader
method ofXMLHttpRequest
object. For example:To sum it up: the address, i.e. the URI of a resource should be independent of its representation and
XMLHttpRequest.setRequestHeader
method allows you to request any representation using theAccept
HTTP header.Cheers!
Shonzilla