json - 休息 HATEOAS : How to determine and set media-type while browsing links?

标签 json http rest mime-types hateoas

我正在浏览 what被描述为 good 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>

最佳答案

简单地说,是的,客户端需要对所涉及的媒体类型有一些先验知识。由于客户端实际上设置了它可以使用的媒体类型。由于客户端只理解“某些”媒体类型,如果它发送的请求带有应用程序不支持的媒体类型,那么客户端就很不走运了。

因为在现实世界中,我们尽量不让客户端盲目调用服务返回他们不理解的有效载荷,客户端将对所涉及的有效载荷有一些预先了解,尤其是非常具体的类型(与普通/文本或应用程序/xml)。

最后,回想一下,媒体类型会有效地告诉您有效载荷的语法,但不会告诉您如何解释有效载荷。您的客户也必须预先了解这些语义,因此初步了解媒体类型的负担实际上并不是参与的特别障碍,这只是生活中的事实。

关于json - 休息 HATEOAS : How to determine and set media-type while browsing links?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15797282/

相关文章:

json - 将 3D 模型转换为 SceneJS JSON,包括纹理

angularjs - 使用 angular 的 $http.put 发送数据错误

http - 每个应用程序需要多少个HTTP客户端?

arrays - 如何在 native react 中将json数据与数组映射

rest - 为 RESTful(超媒体)API 编写客户端

javascript - Php JSON 编码数组和解码中的意外标记错误

java - 在 Spring Controller 中返回未捕获异常的 JSON

javascript - 使用Ajax传递Json变量并解码

html - 强制 Tumblr 使用 http 链接而不是 https

休息 API : Obtaining Payment ID after redirect back to merchant site