根据这个优秀的演讲在设计REST风格的接口,实现版本的首选方法是利用接受头,使用是这样的:
GET /products HTTP/1.1
Host: example.com
Accept: application/vnd.com.myservice.v2+xml
这完全适用于XML的内容类型,但可以使用相同的方案进行版本的JSON-相同呢?
即,是否有可能要求:
GET /products HTTP/1.1
Host: example.com
Accept: application/vnd.com.myservice.v2+json
他们的回应是这样的:
HTTP/1.1 200 OK
Content-Type: application/vnd.com.myservice.v2+xml; charset=UTF-8
Allow: GET, POST
<?xml version="1.0" encoding="utf-8"?>
<products xmlns="urn:com.example.products"
xmlns:xl="http://www.w3.org/1999/xlink">
<product id="1234" xl:type="simple"
xl:href="http://example.com/products/1234">
<name>Red Stapler</name>
<price currency="EUR">3.14</price>
<availability>false</availability>
</product>
</products>
和JSON当量(排序的):
HTTP/1.1 200 OK
Content-Type: application/vnd.com.myservice.v2+json; charset=UTF-8
Allow: GET, POST
[
{
id: "1234",
links: [
{
rel: "self",
href: "http://example.com/products/1234"
}
],
name: "Red Stapler",
price: {
currency: "EUR",
value: 3.14
},
availability: false
}
]