Should the representation(html, xml, json) returned by a RESTful web service be determined by the url or by the Accept HTTP header?
相关问题
- Design RESTful service with multiple ids
- Axios OPTIONS instead of POST Request. Express Res
- Plain (non-HTML) error pages in REST api
- Laravel 5.1 MethodNotAllowedHttpException on store
- Can I parameterize labels and properties on CREATE
相关文章
- Got ActiveRecord::AssociationTypeMismatch on model
- Multiple parameters in AngularJS $resource GET
- Global Exception Handling in Jersey & Spring?
- REST search interface and the idempotency of GET
- Getting error detail from WCF REST
- Send a GET request with a body in JavaScript (XMLH
- GuzzleHttp Hangs When Using Localhost
- JAX-RS (Jersey) ExceptionMapper - @Context injecti
See Chapter 5 - Representational State Transfer (REST), section 5.2.1.2 Representations of Roy Fielding's dissertation on Architectural Styles:
Looking at the link, we see that it refers to MIME. So I assume that in HTTP parlance, it is represented with a
Content-Type
header for POST/PUT andAccept
header for GET.Here is the rest of the paragraph (for completeness):
P.S.: I am not sure why people never look in the place where REST is actually defined...
Use the Accept header if provided, URI as a failover.
Both are valid. Quote from xml.com:
There are problems with using content type... I discussed this on my blog http://shouldersofgiants.co.uk/Blog and finally settled on including the representation in the URI as suggested in RESTful Web Services by Richardson and Ruby
This is a non-question.
Accept depends on conneg (content negotiation). Conneg will let the client decide what media type they accept through the Accept: header. The response will then be in that format, together with a Vary: Accept header.
On the other hand, it's also possible and perfectly valid to expose your resource as /resource.json and /resource.xml.
The ideal is to implement both: /resource (generic uri that supports conneg) /resource.xml /resource.json
the conneg'd version returned by /resource can simply redirect to the correct uri based on the negotiated media type. Alternatively, the correct representation can be returned from the generic uri, and use Content-Location to specify the sepcific representation that was returned.
Since you're mentioning a RESTful web service and not any web service, I would strongly go for what is supported by underlying standard - HTTP 1.1 and its content negotiation that relies on
Accept
HTTP header.As I've explained in my answer to Can I change the headers of the HTTP request send by the browser, address (URI) and representation are two distinct pillars of a RESTful design and they do not need to be mixed. One should not abuse URI for embedding acceptable representations when there's
Accept
header.Only if your web application is potentially run and used in an environment where's some HTTP header filtering involved by intermediate nodes, then you should support URI-based content negotiation. Truth be told, such intrusive or improperly functioning proxies should be replaced if anyhow possible and feasible.
Cheers!
Shonzilla