HTTP POST响应创建多个资源时的位置标头 [英] HTTP POST response Location header when creating multiple resources

查看:170
本文介绍了HTTP POST响应创建多个资源时的位置标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTTP / 1.1标准指出如果 POST 操作导致创建资源,则响应应包含 Location 标头,其地址为新资源。

The HTTP/1.1 standard states that if a POST operation results in the creation of a resource, then the response should include a Location header with the address of the new resource.


如果在源服务器上创建了资源,则响应
应为201(已创建)并且包含一个描述请求的
状态的实体,并引用新资源和一个Location
标题(参见第14.30节)。

If a resource has been created on the origin server, the response SHOULD be 201 (Created) and contain an entity which describes the status of the request and refers to the new resource, and a Location header (see section 14.30).

和14.30节,


对于201(创建)响应,Location是新资源的位置
由请求创建。

For 201 (Created) responses, the Location is that of the new resource which was created by the request.

现在假设我的API允许通过 POST 将数组添加到集合资源URL。例如:

Now suppose that my API allows batch creation of resources by POSTing an array to the collection resource URL. For example:

POST /books
[
    {
        "name": "The Colour of Magic",
        "published": "1983"
    },
    {
        "name": "The Light Fantastic",
        "published": "1986"
    }
]

因为两个 \\ \\ book\ {bookId} 已创建资源,在这种情况下, Location 标题的值应该是多少?

Since two \book\{bookId} resources have been created, what should be the value of the Location header in this case?

问题多个新资源创建后的Http发布回复?是类似的,但它询问的是响应实体,而不是标题(并且没有答案)。

The question Http post response after multiple new resource creation? is similar, but it asks about the response entity, not the headers (and is unanswered).

推荐答案

我知道这个答案迟到了,但我相信最好的解决方案是使用uuid标识符创建一个新的Batches资源,该标识符将返回使用如下URL添加的Book URL列表:

I know this answer is late to the party but I believe the best solution is to create a new Batches resource with a uuid identifier that would return the list of Book URLs that were added using a URL like this:

http://api.example.com/batches/{uuid}

例如

http://api.example.com/batches/2b9b251f71a4b2901d66e04725bc0c9cb5843c74

然后您的 POST PUT 可以返回上面的网址位置:{url} 标题和 201 - 创建的状态代码。

Then your POST or PUT can return the above URL on it's Location: {url} header and a 201 - Created status code.

如果你然后 GET 该网址,那么它将在该批次以及任何其他网址中创建的网址列表有关批次的信息,例如其uuid及其创建的时间/日期。

If you then GET that URL it would then the list of URLs created in that batch as well as any other info about the batch such as its uuid and the time/date it was created.

{
  "uuid": "2b9b251f71a4b2901d66e04725bc0c9cb5843c74",
  "datetime": "2005-08-15T15:52:01+00:00",
  "books": [
    "http://api.example.com/books/the-colour-of-magic",
    "http://api.example.com/books/the-light-fantastic"
  ]
}

无论您选择什么,这些资源都可以有一个小时或一个月的TTL。如果你愿意,他们可以永远活着;无论你的用例如何。

Those resources could then have a TTL of an hour or a month, whatever you choose. Of they could live forever if you want; whatever your use-case requires.

这篇关于HTTP POST响应创建多个资源时的位置标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆