位置与 Content-Location 的 post 请求

Location vs Content-Location on post requests

我对 header LocationContent-Location 感到困惑。

Content-Location

关于Content-Location我在spec中找到了这个说法。 (强调我的)

For a state-changing request like PUT (Section 4.3.4) or POST (Section 4.3.3), it implies that the server's response contains the new representation of that resource, thereby distinguishing it from representations that might only report about the action (e.g., "It worked!"). This allows authoring applications to update their local copies without the need for a subsequent GET request.

然而,在 mdn 文档中 example 显示了相反的行为。 (强调我的)

The site returns a generic success message confirming the post was published. The server specifies where the new post is with Content-Location:

HTTP/1.1 201 Created
Content-Type: text/plain; charset=utf-8
Content-Location: /my-first-blog-post

✅ Success!

这两种说法似乎相互矛盾。

现在,对于 在响应中包含真实实体的情况,我不确定是否应该使用 Content-Location。


位置

spec 有一句关于 Location header.

For 201 (Created) responses, the Location value refers to the primary resource created by the request.

和mdn saying一样。

In cases of resource creation, it indicates the URL to the newly created resource.


对于不包含实体的 post 响应,我很困惑该选择哪一个。

我猜 Location header 最适合通用 post 请求,稍后可以查看实体。例如,post访问一个用户并查看它。

带有 202 代码的 POST 响应怎么样?例如,当 post 将任务发送到 queue 时,稍后只能查看任务的状态。所以它不是像用户这样的真实实体。 IE。根据 posted 任务向 X 客户端发送了一封电子邮件,现在我想告知可以查看状态(待处理、失败或成功)的位置。

你是对的,MDN 关于第一个例子是错误的。这个例子:

HTTP/1.1 201 Created
Content-Type: text/plain; charset=utf-8
Content-Location: /my-first-blog-post

✅ Success!

提示/my-first-blog-post的内容是✅ Success!,由于Content-Locationheader.

鉴于您不想return新资源的body,您应该保留Location并省略[=13] =].

如果您有时间:将问题报告给 MDN 维护者。

What about a POST response with a 202 code? For example, when posting a task to a queue where later only the status of the task can be viewed. So it isn't a real entity like a user. I.e. an email was dispatched to X clients based on the posted task, now I want to communicate where the status (PENDING, FAILURE or SUCCESS) can be viewed.

我建议 Link header.