成功的 HTTP POST
请求后,客户端可以跟踪响应 header 中包含的 URI,名为 Location
。
我想知道是否允许不提供此 header Location
,例如在单例资源上,例如:
POST /users/42/profile
...因为在此请求之后,如果成功(状态代码 201
),客户端已经知道创建的资源的位置,因此再次重定向是没有用的它指向当前的 URI。
在这种情况下,Location
响应 header 是可选的吗?是否可以不提供给HTTP客户端?
最佳答案
根据 HTTP spec
14.30 Location
The Location response-header field is used to redirect the recipient to a location other than the Request-URI for completion of the request or identification of a new resource. For 201 (Created) responses, the Location is that of the new resource which was created by the request.
新创建的资源通常从响应链接到 有效负载,最相关的 URI 也包含在 位置 header 字段。如果新创建的资源的 URI 是 与有效请求URI相同,该信息可以省略 (例如,在响应 PUT 请求的情况下)
因此您可以省略“位置” header 。
关于http - 响应不带位置的 HTTP POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29202992/