HTTP:POST请求收到302,重定向请求是否为GET?

时间:2011-11-15 14:45:03

标签: http post redirect get http-status-code-302

我正在阅读this但我并没有真正从那里得到重定向请求应该具有的请求类型,即函数(初始请求类型,响应类型) - >重定向请求型。

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

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

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

在Python库requests中,有以下代码(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

即,在303(codes.see_other)的情况下,redirect-request-type是GET,在所有其他情况下,它是初始请求类型。即,对于我上面的特定情况,它将是POST,与Chrome相反。

这可能是错误的,因为我有一个网站,其实际上似乎没有正确的工作(即网站没有这种方式表现良好)。

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

4 个答案:

答案 0 :(得分:18)

我刚刚在Chrome中搜索了相关代码,而here则是:

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;
}

答案 1 :(得分:7)

根据RFC 2616,答案是"原始方法"。 HTTPbis会对此进行修改,因为它没有反映出浏览器的作用(遗憾的是)。

有关历史记录,请参阅http://trac.tools.ietf.org/wg/httpbis/trac/ticket/160

答案 2 :(得分:1)

除了303和307之外,根据spec,任何一种行为都是可以接受的,主要是出于历史原因。

答案 3 :(得分:0)

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

它就是它,它可能永远不会改变。感谢所有以前的响应者提供所有相关信息。