【发布时间】:2013-11-09 06:29:27
【问题描述】:
我已经开始设计 API,并决定尝试使其符合 REST/HATEOAS。 API 的入口点应该是什么?
GET / 似乎很常见,但从我读到的内容来看,使用OPTIONS / 在逻辑上可能更有意义,因为/ 实际上没有用于检索的资源。
我在这里给出了这两个例子,使用 JSON 的 HAL 语法作为超媒体格式。
获取 /
请求:
GET / HTTP/1.1
Host: example.com
回应:
HTTP/1.1 200 OK
Date: …
Content-Type: application/json;charset=utf-8
Content-Length: 143
{
"_links": {
"self": {
"href": "/"
},
"penguins": {
"href": "/penguins"
}
}
}
选项/
请求:
OPTIONS / HTTP/1.1
Host: example.com
回应:
HTTP/1.1 200 OK
Date: …
Allow: OPTIONS
Content-Type: application/json;charset=utf-8
Content-Length: 143
{
"_links": {
"self": {
"href": "/"
},
"penguins": {
"href": "/penguins"
}
}
}
【问题讨论】:
-
为什么你认为
/没有指向资源?在 REST 中,所有 URL 都引用资源。在这种情况下,它是您可以关注的可用链接菜单的资源。
标签: rest entry-point hateoas http-options-method hal-json