HTTP:POST 请求收到 302,重定向请求应该是 GET 吗?

发布于 2024-12-15 19:13:32 字数 849 浏览 3 评论 0原文

我正在阅读,但我并没有真正从那里得到什么请求- type 在什么情况下redirect-request应该有,即函数(初始request-type,response-type)->重定向请求类型。

在我的特定情况下,我有:

  • 初始请求类型:POST
  • 响应类型:302

Google Chrome 使用 GET 进行重定向请求。

在Python库requests中,有以下代码(此处):

# http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4
if r.status_code is codes.see_other:
    method = 'GET'
else:
    method = self.method

即,在 303 的情况下,重定向请求类型为 GET (codes.see_other),在所有其他情况下它是初始请求类型。即,对于我上面的特定情况,它将是 POST,与 Chrome 不同。

这可能是错误的,因为我有一个网站,但实际上似乎无法正常工作(即该网站以这种方式表现不佳)。

正确的方法/功能是什么?

I was reading this but I didn't really got from there what request-type the redirect-request should have in what case, i.e. the function (initial request-type, response-type) -> redirect-request-type.

In my particular case, I had:

  • initial request-type: POST
  • response-type: 302

Google Chrome used a GET for the redirected request.

In the Python library requests, there is the following code (here):

# http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4
if r.status_code is codes.see_other:
    method = 'GET'
else:
    method = self.method

I.e., the redirect-request-type is GET in case of 303 (codes.see_other), in all other cases it is the initial request-type. I.e., for my particular case above, it would be POST, in contrast to Chrome.

This is probably wrong because I have one website where this actually doesn't seem to work correct (i.e. the website doesn't behave well this way).

What would be the correct way/function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝眼泪 2024-12-22 19:13:32

我刚刚在Chrome中搜索了相关代码,这里< /a> 它是:

std::string ComputeMethodForRedirect(const std::string& method,
                                     int http_status_code) {
  // For 303 redirects, all request methods except HEAD are converted to GET,
  // as per the latest httpbis draft.  The draft also allows POST requests to
  // be converted to GETs when following 301/302 redirects, for historical
  // reasons. Most major browsers do this and so shall we.  Both RFC 2616 and
  // the httpbis draft say to prompt the user to confirm the generation of new
  // requests, other than GET and HEAD requests, but IE omits these prompts and
  // so shall we.
  // See:
  // https://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-17#section-7.3
  if ((http_status_code == 303 && method != "HEAD") ||
      ((http_status_code == 301 || http_status_code == 302) &&
       method == "POST")) {
    return "GET";
  }
  return method;
}

I just searched for the relevant code in Chrome, and here it is:

std::string ComputeMethodForRedirect(const std::string& method,
                                     int http_status_code) {
  // For 303 redirects, all request methods except HEAD are converted to GET,
  // as per the latest httpbis draft.  The draft also allows POST requests to
  // be converted to GETs when following 301/302 redirects, for historical
  // reasons. Most major browsers do this and so shall we.  Both RFC 2616 and
  // the httpbis draft say to prompt the user to confirm the generation of new
  // requests, other than GET and HEAD requests, but IE omits these prompts and
  // so shall we.
  // See:
  // https://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-17#section-7.3
  if ((http_status_code == 303 && method != "HEAD") ||
      ((http_status_code == 301 || http_status_code == 302) &&
       method == "POST")) {
    return "GET";
  }
  return method;
}
深巷少女 2024-12-22 19:13:32

根据 RFC 2616,答案是“原始方法”。 HTTPbis 将对此进行修改,因为它不反映浏览器的功能(遗憾的是)。

请参阅http://trac.tools.ietf.org/wg/httpbis/trac/ticket/160 的历史记录。

Per RFC 2616, the answer is "the original method". HTTPbis will revise this, as it doesn't reflect what browsers do (sadly).

See http://trac.tools.ietf.org/wg/httpbis/trac/ticket/160 for the history.

养猫人 2024-12-22 19:13:32

除了 303 和 307 之外,根据 spec,主要是历史原因。

Except for 303 and 307, either behaviour is acceptable as per the spec, mainly for historical reasons.

梨涡少年 2024-12-22 19:13:32

在使用 Chrome 和节点请求体验之后,我思考了这个问题的答案是什么,并最初假设这是完全正常的。然后我想,虽然它可能是“历史的”,但它可能不是“正确的”。所以我找到了这个页面,听起来“正确”并不比与“历史”实现兼容重要......这听起来令人失望一分钟。然后我想起我见过的每个“传统”、非 Ajax/API、基于表单的“POST”都会以假定为 GET 的重定向进行响应。

它就是这样,并且可能永远不会改变。感谢之前所有回复者提供的所有相关信息。

I thought about what the answer to this question was after experiencing it with Chrome and node-requests, and initially assuming that it was totally normal. Then I thought that while it may be "historical", it wasn't probably "correct". So I found this page, and it sounds like being "correct" is less important than being compatible with "historical" implementations...which sounded disappointing for a minute. Then I remembered that every "traditional", non-Ajax/API, form based "POST" I have ever seen responds with a redirect that assumes a GET.

It is what it is and that's probably not changing ever. Thanks to all the previous responders for providing all the relevant info.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文