使用 HTTP Location header 指示未来资源的位置
using HTTP Location header to indicate the location of a future resource
遵循 Richardson maturity model by Martin Fowler 中描述的 REST Level 2 规范,对创建资源的 POST 请求的响应如下所示:
HTTP/1.1 201 Created
Location: slots/1234/appointment
[various headers]
<appointment>
<slot id = "1234" doctor = "mjones" start = "1400" end = "1450"/>
<patient id = "jsmith"/>
</appointment>
我想遵循此模型,但在我的应用程序中,当调用端点以创建新资源时,它会添加到 queue 而不是立即创建,因此我使用 HTTP 202 进行响应201.
我还想包含一个 URL,指示一旦 queue 处理完毕,用户可以获取的内容的未来位置。我可以利用 Location HTTP header 用于此吗?
结果类似于:
HTTP/1.1 202 Accepted
Location: slots/1234/appointment
在 MSDN 上声明了关于位置 header:
It only provides a meaning when served with a 3xx (redirection) or
201 (created) status response.
如果是这种情况,还有什么其他标准化 HTTP 替代方案可用于这种情况?
RFC 7230:
The status-code element is a 3-digit integer code describing the result of the server's attempt to understand and satisfy the client's corresponding request. The rest of the response message is to be interpreted in light of the semantics defined for that status code.
所以Location
header的意思要根据202 Accepted
语义来解释。
The "Location" header field is used in some responses to refer to a specific resource in relation to the response. The type of relationship is defined by the combination of request method and status code semantics.
For 201 (Created) responses, the Location value refers to the primary resource created by the request. For 3xx (Redirection) responses, the Location value refers to the preferred target resource for automatically redirecting the request.
据我所知,在 202 Accepted
的上下文中,Location 没有任何已定义的语义含义,也就是说,通用组件将不知道如何解释您的意图。
当我们查看 202 Accepted 的语义时,我们发现:
The representation sent with this response ought to describe the request's current status and point to (or embed) a status monitor that can provide the user with an estimate of when the request will be fulfilled.
人们可能会合理地预期 202 Accepted
响应的“相关资源”将是 状态监视器 ,而不是 [=42] 的最终目标=] 操作。那里有足够的歧义,我不会尝试使用 Location
机制。
Web Linking is probably a better solution for your problem; it gives you a standardized way of communicating an arbitrary relationship between resources. You can review the registry to see if there is a registered relation that has the semantics that you need, or you can define your own extension relation.
超文本媒体类型将有自己的机制来描述 link 语义(例如,HTML 中的“rel”属性)。但是还有一个header你可以用。
注意:注册关系不会在平板电脑上流传下来;您可以通过注册程序来创建满足您的用例的新注册关系。我的猜测是,首先对作为扩展关系的语义进行一些实际体验将使注册过程更加顺利。
遵循 Richardson maturity model by Martin Fowler 中描述的 REST Level 2 规范,对创建资源的 POST 请求的响应如下所示:
HTTP/1.1 201 Created
Location: slots/1234/appointment
[various headers]
<appointment>
<slot id = "1234" doctor = "mjones" start = "1400" end = "1450"/>
<patient id = "jsmith"/>
</appointment>
我想遵循此模型,但在我的应用程序中,当调用端点以创建新资源时,它会添加到 queue 而不是立即创建,因此我使用 HTTP 202 进行响应201.
我还想包含一个 URL,指示一旦 queue 处理完毕,用户可以获取的内容的未来位置。我可以利用 Location HTTP header 用于此吗? 结果类似于:
HTTP/1.1 202 Accepted
Location: slots/1234/appointment
在 MSDN 上声明了关于位置 header:
It only provides a meaning when served with a 3xx (redirection) or 201 (created) status response.
如果是这种情况,还有什么其他标准化 HTTP 替代方案可用于这种情况?
RFC 7230:
The status-code element is a 3-digit integer code describing the result of the server's attempt to understand and satisfy the client's corresponding request. The rest of the response message is to be interpreted in light of the semantics defined for that status code.
所以Location
header的意思要根据202 Accepted
语义来解释。
The "Location" header field is used in some responses to refer to a specific resource in relation to the response. The type of relationship is defined by the combination of request method and status code semantics.
For 201 (Created) responses, the Location value refers to the primary resource created by the request. For 3xx (Redirection) responses, the Location value refers to the preferred target resource for automatically redirecting the request.
据我所知,在 202 Accepted
的上下文中,Location 没有任何已定义的语义含义,也就是说,通用组件将不知道如何解释您的意图。
当我们查看 202 Accepted 的语义时,我们发现:
The representation sent with this response ought to describe the request's current status and point to (or embed) a status monitor that can provide the user with an estimate of when the request will be fulfilled.
人们可能会合理地预期 202 Accepted
响应的“相关资源”将是 状态监视器 ,而不是 [=42] 的最终目标=] 操作。那里有足够的歧义,我不会尝试使用 Location
机制。
Web Linking is probably a better solution for your problem; it gives you a standardized way of communicating an arbitrary relationship between resources. You can review the registry to see if there is a registered relation that has the semantics that you need, or you can define your own extension relation.
超文本媒体类型将有自己的机制来描述 link 语义(例如,HTML 中的“rel”属性)。但是还有一个header你可以用。
注意:注册关系不会在平板电脑上流传下来;您可以通过注册程序来创建满足您的用例的新注册关系。我的猜测是,首先对作为扩展关系的语义进行一些实际体验将使注册过程更加顺利。