我将一个响应POST请求的API放在一起,方法是将新资源的内容放在响应主体中,并将新资源的URL放在Location HTTP响应头中.
样品申请:
POST /api/v1/widgets HTTP/1.1
Content-type: application/json;
Accept: application/json;
{
"name": "hugo@example.com",
"price": "10",
}
Run Code Online (Sandbox Code Playgroud)
样品回复:
HTTP 201 Created
Location: http://example.com/api/v1/widgets/123456
{
'widget':
{
'id': "123456",
'created': "2012-06-22T12:43:37+0100",
'name': "hugo@example.com",
'price': "10",
},
}
Run Code Online (Sandbox Code Playgroud)
有人提出了一个问题,即URL也应该在响应的正文中.这是最好的做法吗?
我会把它放在标题中(如位置:http://blah.blah.com/blah).你可以把它放在你的身体里(如果你想要的任何适当的格式),它也不会是不合适的.
该AtomPub的REST API,通常是一个很好的REST API一个很好的参考.他们把它放在两者中.
HTTP/1.1 201 Created
Date: Fri, 7 Oct 2005 17:17:11 GMT
Content-Length: nnn
Content-Type: application/atom+xml;type=entry;charset="utf-8"
Location: http://example.org/edit/first-post.atom
ETag: "c180de84f991g8"
<?xml version="1.0"?>
<entry xmlns="http://www.w3.org/2005/Atom">
<title>Atom-Powered Robots Run Amok</title>
<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
<updated>2003-12-13T18:30:02Z</updated>
<author><name>John Doe</name></author>
<content>Some text.</content>
<link rel="edit"
href="http://example.org/edit/first-post.atom"/>
</entry>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17535 次 |
| 最近记录: |