简体   繁体   English

是否允许HTTP POST请求发送回响应正文?

[英]Is HTTP POST request allowed to send back a response body?

As per the HTTP specification: 根据HTTP规范:

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). 如果在源服务器上创建了资源,则响应应该是201(已创建)并包含描述请求状态的实体,并引用新资源和Location头(请参阅第14.30节)。

Does this mean that POST request should always send redirect URI in Location header with no response body? 这是否意味着POST请求应始终在Location标头中发送重定向URI而没有响应主体?

It is perfectly acceptable to specify a response body and use the Location header at the same time. 指定响应主体并同时使用Location标头是完全可以接受的。 When using the Location header with a 201 response, you're not redirecting the client, you're just telling it where it can find the resource in future. 当使用带有201响应的Location标头时,您不会重定向客户端,您只是告诉它将来可以在哪里找到该资源。

Redirects only apply to 3xx responses. 重定向仅适用于3xx响应。

The W3C docs for this explain further, though the text is actually quite ambiguous: W3C文档为此进一步解释,尽管文本实际上很模糊:

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. Location response-header字段用于将收件人重定向到Request-URI以外的位置,以完成请求或标识新资源。 For 201 (Created) responses, the Location is that of the new resource which was created by the request. 对于201(已创建)响应,Location是请求创建的新资源的位置。 For 3xx responses, the location SHOULD indicate the server's preferred URI for automatic redirection to the resource. 对于3xx响应,位置应该应该指示服务器自动重定向到资源的首选URI。

I read that as saying "...redirect... or... identif[y]... new resource", but it's not exactly a plain English sentence. 我读到这就是说“...重定向......或......识别[y] ......新资源”,但这并不是一个简单的英语句子。

Based on paragraph 9.5 of the HTTP 1.1 specification , which is the reference for questions like that, here is my understanding: 基于HTTP 1.1规范的第9.5段 ,这是这类问题的参考,这是我的理解:

Yes you can, and the specification is clear about what you can do and how to do it: 是的,你可以,并且规范清楚你可以做什么以及如何做:

The action performed by the POST method might not result in a resource that can be identified by a URI. POST方法执行的操作可能不会生成可由URI标识的资源。 In this case, either 200 (OK) or 204 (No Content) is the appropriate response status, depending on whether or not the response includes an entity that describes the result. 在这种情况下, 200 (OK)或204 (No Content)是适当的响应状态,具体取决于响应是否包括描述结果的实体。

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). 如果在源服务器上创建了资源,则响应应该是201 (已创建)并包含描述请求状态的实体,并引用新资源和Location头(请参阅第14.30节)。

Responses to this method are not cacheable, unless the response includes appropriate Cache-Control or Expires header fields. 除非响应包含适当的Cache-ControlExpires头字段,否则对此方法的响应不可缓存。 However, the 303 (See Other) response can be used to direct the user agent to retrieve a cacheable resource. 但是, 303 (请参阅其他)响应可用于指示用户代理检索可缓存资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM