2011-11-15 151 views
16

我在读this,但我并没有真正从那里得到什么请求类型的重定向请求应该在什么情况下,即函数(初始请求类型,响应类型) - >重定向请求类型。HTTP:如果重定向请求是GET请求,POST请求会收到302?

在我的具体情况,我有:

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

谷歌浏览器中使用的查看该重定向请求。

在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)情况下GET,在所有其他情况下,它是最初的请求类型。也就是说,对于我上面的特定情况,它将是POST,而不是Chrome。

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

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

回答

1

除303和307之外,主要由于历史原因,任何行为都可以接受,如spec

+1

呃,如果每个浏览器的行为都不一样,网站不能这样工作,那么不遵守规范就太明智了? – Albert

+0

嗯,严格遵守规范并让每个浏览器的供应商遵守规范更明智吗? –

+0

这里的规格比较宽松。 –

14

我只是搜索在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; 
} 
0

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

这是什么,这可能不会改变。感谢所有以前的响应者提供所有相关信息。