简体   繁体   English

为什么Go http.Client中的POST请求不遵循301重定向?

[英]Why do POST request in the Go http.Client not follow 301 redirects?

I'm building a test tool with Go. 我正在使用Go构建测试工具。 This tool can retrieve a specific URL by doing a POST request to an endpoint which returns a 303 with the Location to test. 该工具可以通过对端点执行POST请求来获取特定的URL,该端点将返回带有测试Location的303。 Sometimes this location itself is redirected with a 301 which I want to follow as well. 有时,此位置本身也以301重定向,我也想遵循。

Test tool -> POST /get-url-to-test -> 303 Location: /other -> GET /other -> 301 Location: /new-other (stops here because initial request is POST)

As we can see in Go's source (lines 241 to 257), it seems like GET requests follow 301 redirects, but not POST requests: http://golang.org/src/net/http/client.go 正如我们在Go的源代码(第241至257行)中所看到的那样,似乎GET请求遵循301重定向,而不是POST请求: http : //golang.org/src/net/http/client.go

Why is that? 这是为什么? Is that part of an HTTP spec? 这是HTTP规范的一部分吗? Is this an decision that was made by the Go community? 这是Go社区做出的决定吗?

The reason I'm asking is because in my case, I'd have to manually do a new GET request to get to the URL redirected to by /other , I think. 我问的原因是因为就我而言,我必须手动执行一个新的GET请求才能到达/other重定向到的URL。


EDIT 1: I made a mistake before: the /other resource is being fetched by Go with a GET request. 编辑1:之前我犯了一个错误:Go通过GET请求获取了/other资源。 But since it returns a 301 and the initial request was a POST, Go stops redirecting on the 301. That seems odd. 但是由于返回了301并且初始请求是POST,所以Go停止了在301上的重定向。这似乎很奇怪。 Am I missing something? 我想念什么吗?

EDIT 2: This may be a bug, I've opened an issue on Github: https://github.com/golang/go/issues/9348 编辑2:这可能是一个错误,我在Github上打开了一个问题: https : //github.com/golang/go/issues/9348

The HTTP RFC 2616 says: HTTP RFC 2616说:

10.3 Redirection 3xx 10.3重定向3xx

This class of status code indicates that further action needs to be taken by the user agent in order to fulfill the request. 此类状态码表示用户代理需要采取进一步的措施才能满足请求。 The action required MAY be carried out by the user agent without interaction with the user if and only if the method used in the second request is GET or HEAD . 当且仅当第二个请求中使用的方法是GET或HEAD时,才可以由用户代理执行所需的操作,而无需与用户进行交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM