首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >HTTP: POST请求收到302,重定向请求是否应为GET?

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

Stack Overflow用户
提问于 2011-11-15 22:45:03
回答 4查看 25.6K关注 0票数 19

我在读this,但是我不知道在什么情况下重定向请求应该有什么样的请求类型,比如函数(初始请求类型,响应类型) ->重定向请求类型。

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

  • 初始请求类型: POST
  • response-type: 302

Google Chrome对重定向的请求使用了GET。

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

代码语言:javascript
运行
复制
# 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)的情况下,重定向请求类型是GET,在所有其他情况下,它是初始请求类型。也就是说,对于我上面的特殊情况,它将是POST,而不是Chrome。

这可能是错误的,因为我有一个网站,实际上这似乎是不正确的工作(即网站不能很好地这样做)。

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

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-11-15 23:59:34

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

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

票数 7
EN

Stack Overflow用户

发布于 2011-11-15 23:08:31

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

代码语言:javascript
运行
复制
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;
}
票数 23
EN

Stack Overflow用户

发布于 2011-11-15 22:57:09

除了303和307之外,根据spec,这两种行为都是可接受的,主要是由于历史原因。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8138137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档