gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 02:05:39 26 4
gpt4 key购买 nike

我正在使用 Go 构建一个测试工具。此工具可以通过对端点执行 POST 请求来检索特定的 URL,该端点返回 303 和要测试的 Location。有时,此位置本身会使用 301 重定向,我也想跟随它。

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

正如我们在 Go 的源代码(第 241 到 257 行)中看到的那样,GET 请求似乎遵循 301 重定向,但 POST 请求没有: http://golang.org/src/net/http/client.go

这是为什么呢?那是 HTTP 规范的一部分吗?这是 Go 社区做出的决定吗?

我问的原因是因为在我的情况下,我认为我必须手动执行新的 GET 请求才能到达 /other 重定向到的 URL。


编辑 1:我之前犯了一个错误:/other 资源正在被 Go 使用 GET 请求获取。但是因为它返回 301 并且初始请求是 POST,Go 停止在 301 上重定向。这看起来很奇怪。我错过了什么吗?

编辑 2:这可能是一个错误,我在 Github 上打开了一个问题:https://github.com/golang/go/issues/9348

最佳答案

HTTP RFC 2616说:

10.3 Redirection 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.

关于http - 为什么 Go http.Client 中的 POST 请求不遵循 301 重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27505066/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com