Type: Should it be based on extension or Accept header?
应由REST风格的Web服务返回的表示(html,xml,json)由url还是由Accept HTTP头确定?
Both are valid. Quote from xml.com:
A resource may have more than one representation. There are four frequently used ways of delivering the correct resource representation to consumers:
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
上一篇: REST API身份验证