我正在经历被描述为一个好的 REST API的例子。GET 是在基本 URI 上发送的,并且带有客户端已经知道的媒体类型(这很好,根据 REST 原则)。
To server:
GET /
Host: xrgy.cloud.sun.com
Authorization: Basic xxxxxxxxxxxxxxxxxxx
Accept: application/vnd.com.sun.cloud.Cloud+json
X-Compute-Client-Specification-Version: 0.1
From Server:
HTTP/1.1 200 OK
Content-Type: application/vnd.com.sun.cloud.Cloud+json
Content-Length: nnn
{
"implementation_version": "597",
"vdcs": [
{
"name": "XRGY Virtual Data Center",
"uri": "/vdc"
}
{
"name": "R&D sandbox"
"uri": "/sandbox"
}
],
"uri": "http://xrgy.cloud.sun.com/",
"specification_version": [
"0.5"
]
}
但我陷入困境的是客户端如何为后续请求设置媒体类型。我知道客户端从上一个响应中获得了下一个请求的 URI。但是它是从哪里获得媒体类型的呢?如果它是客户端的先验知识,那么客户端通常如何维护这样的 URI:media-type 映射?看来我肯定在这里错过了一些基本知识。以下是使用媒体类型发送的后续请求:application/vnd.com.sun.cloud.Vdc+json!
To server:
GET /vdc
Host: xrgy.cloud.sun.com
Authorization: Basic xxxxxxxxxxxxxxxxxxx
Accept: application/vnd.com.sun.cloud.Vdc+json
X-Compute-Client-Specification-Version: 0.1
From server:
HTTP/1.1 200 OK
Content-Type: application/vnd.com.sun.cloud.Vdc+json
Content-Length: nnn
{
"name" : "XRGY Virtual Data Center",
"uri" : "http://xrgy.cloud.sun.com/vdc",
"vm_templates" : "http://cloud.sun.com/resources/template-cat.json",
"addresses" : [
{
"name": "144.34.100.199",
"uri": "/addresses/144.34.100.199",
"ip_address": "144.34.100.199"
}
],
"cluster" : {
"name" : "ROOT",
"uri" : "/vdc/",
"tags" : [ ],
"volumes" : [ ],
"clusters" : [
]
"tags" : [ ],
"controllers" : [
"start" : "/vdc/ops/start",
"stop" : "/vdc/ops/stop",
]
"vnets" : [
{
"name": "vnet1",
"uri": "/vnets/10.31.145.0",
"netmask": "255.255.255.0",
"network": "10.31.145.0"
}
],
"vms": [
{
* SNIPPED *
}
]
}
}
我已经看到其他示例,其中媒体类型也是响应中链接的一部分,例如以下响应,我可以理解。
201 Created
Content-Type: application/vnd.bank.org.transfer+xml;charset=UTF-8
<transfer xmlns="urn:org:bank:accounts">
<link rel="self"
href="http://bank.org/transfer/XTA8763"/>
<link rel="http://bank.org/rel/transfer/from"
type="application/vnd.bank.org.account+xml"
href="http://bank.org/account/AZA12093"/>
<link rel="http://bank.org/rel/transfer/to"
type="application/vnd.bank.org.account+xml"
href="http://bank.org/account/ADK31242"/>
<link rel="http://bank.org/rel/transfer/status"
type="application/vnd.bank.org.status+xml"
href="http://bank.org/check/XTA8763"/>
<id>transfer:XTA8763</id>
<amount currency="USD">100</amount>
<note>RESTing</note>
</transfer>