http - HTTP POST => 302 重定向到 GET 的正确行为是什么?

标签 http redirect http-status-codes

POST => 302 重定向到 GET 的正确行为是什么?

在 chrome(可能大多数浏览器)中,在我 POST(到需要我重定向的资源)并收到 302 重定向后,浏览器会自动在 302 位置发出 GET。这甚至是一个 well known pattern .但我阅读规范的方式似乎表明这不应该发生。

The HTTP spec says

If the 302 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.

fiddler 正在显示:

REQUEST 1: POST URLA
RESPONSE 1: 302 redirect to URLB
REQUEST 2: GET URLB

上面这一段好像是说浏览器不应该发出GET请求?我错过了什么?

  1. 规范中较早的内容使本节无关紧要
  2. 我对自动重定向的理解是错误的(执行 GET 的 chrome 浏览器并没有真正自动重定向)
  3. 我作为用户的理解证实了这一点
  4. 还有别的吗?

最佳答案

规范的下一行开始:

Note: RFC 1945 and RFC 2068 specify that the client is not allowed to change the method on the redirected request. However, most existing user agent implementations treat 302 as if it were a 303 response, performing a GET on the Location field-value regardless of the original request method. The status codes 303 and 307 have been added for servers that wish to make unambiguously clear which kind of reaction is expected of the client.

紧接着,它解释了应该如何处理 303,这正是您所看到的。


如果您问为什么服务器仍然使用 302 而不是 307,所有当前的浏览器都可以正确处理,那是因为旧浏览器无法处理它。如果您想知道为什么浏览器将 302 作为 303 处理,那是因为旧服务器期望它。确实没有办法跳出这个循环,对于 HTTP 来说,将 302 还原为它过去的意思并弃用它(对于非 GET/HEAD)以支持 307 可能会更好。

关于http - HTTP POST => 302 重定向到 GET 的正确行为是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17605915/

相关文章:

java - 如何通过Java Spring Webflux发送多个POST请求

c# - 外部身份验证失败时返回哪个 HTTP 状态?

asp.net-mvc - 在 ASP.NET MVC Controller 结果中设置 HTTP 状态不会呈现 View

java.lang.IllegalStateException : Request cannot be executed; I/O reactor status: STOPPED 错误

c - 我自己写了一个http服务器,我想通过这个服务器执行一个cgi脚本。但我不知道该怎么做

javascript - Chrome 扩展认证

c# - 重写 Url 的 .Net 3.5 Web.config - 重定向

apache - .htaccess 隐藏扩展名,同时强制 www

.htaccess - RewriteRule 给我的正则表达式带来问题

java - 带有状态代码的 Jersey Viewable