HTTP 状态 201 已创建:Return N 个指向新资源的 URL?

HTTP Status 201 Created: Return N URLs to the new resources?

我有一个简单的服务器API:

客户端可以上传文件。服务器 returns 201 Created 如果成功。 http 响应有一个 Location header 指向新创建的资源。

现在我们面临一个新的用例:一个http请求应该能够创建N个新资源。

我不确定如何根据 http 规范实现它。

据我所知,最多只有一个 Location header。

如何在 http 201 Created 响应中 return N 个 URL?

我的问题是相关的,但不是以下问题的重复,因为那里的问题已用 "No" 明确回答。我的问题不同:如何...

我的问题不是"Is it allowed to return several Location header?"。

Can the Location header be used for multiple resource locations in a 201 Created response?

How to return N URLs in a http 201 Created response?

简答:在 message-body.

更长的答案:

我认为您的搜索有些倒退。

在 HTTP 中;状态代码和响应 header 是 元数据 ;伴随消息为通用 http 组件提供上下文不可知的方式来理解发生的事情。

从广义上讲,元数据是从数据中派生出来的。所以从有效载荷开始。

201 CREATED 响应的情况下:

The 201 response payload typically describes and links to the resource(s) created.

您的第一步是创建此表示;如果您在 HTML 中工作,它看起来像是一个网页,其中包含“恭喜,一切正常”和一堆带有超链接的标记文本,以便为客户提供对新资源的访问权限。

完成 之后,您现在可以查看该表示,了解应将哪些信息提升到标准 header 中,以便通用组件也可以理解其中的一些内容进行中。

例如,查看 POST 回复的描述

If one or more resources has been created on the origin server as a result of successfully processing a POST request, the origin server SHOULD send a 201 (Created) response containing a Location header field that provides an identifier for the primary resource created (Section 7.1.2) and a representation that describes the status of the request while referring to the new resource(s).

因此我们确定创建的哪个资源是主要,并将该资源的标识符从表示中提取到位置header。

辅助资源的标识符在消息中仍然可用 body。如果您还需要将这些资源公开给通用组件,那么您可以使用 Link header.

你可以看看 rfc4918, which proposed a new type of response, 207 (Multi-Status), short description copied from REST & WOA Wiki:

A Multi-Status response conveys information about multiple resources in situations where multiple status codes might be appropriate. The default Multi-Status response body is a text/xml or application/xml HTTP entity with a 'multistatus' root element. Further elements contain 200, 300, 400, and 500 series status codes generated during the method invocation. 100 series status codes SHOULD NOT be recorded in a 'response' XML element.