http - 我可以在重定向请求时检查响应吗?

标签 http go

我们可以注册CheckRedirect重定向请求时检查下一个请求。有没有办法在第一个请求重定向时获得响应?

最佳答案

它目前的实现方式,默认情况下似乎无法查看响应(除非您自己实现 Do() 的功能)。
参见 src/net/http/client.go#L384-L399 :

if shouldRedirect(resp.StatusCode) {
        // Read the body if small so underlying TCP connection will be re-used.
        // No need to check for errors: if it fails, Transport won't reuse it anyway.
        const maxBodySlurpSize = 2 << 10
        if resp.ContentLength == -1 || resp.ContentLength <= maxBodySlurpSize {
            io.CopyN(ioutil.Discard, resp.Body, maxBodySlurpSize)
        }
        resp.Body.Close()
        if urlStr = resp.Header.Get("Location"); urlStr == "" {
            err = fmt.Errorf("%d response missing Location header", resp.StatusCode)
            break
        }
        base = req.URL
        via = append(via, req)
        continue
    }

关于http - 我可以在重定向请求时检查响应吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29865691/

相关文章:

php - 从 PHP 将 Http 状态代码返回给 Apache?

rest - 尝试授权 Axios get 请求时出错

sql - 获取指向结构字段值的指针

go - 无法在 Go 中使用 httputil.ReverseProxy 同时使用自定义 RoundTrip 和 Dial

对 http 请求 header 值的格式感到困惑吗?

python - 如何为 python http 连接指定经过身份验证的代理?

sql - 如何使用 golang 捕获新 postgreSQL 记录的事件

go - 如何在golang中获取项目中的包数

arrays - 在 GoLang 和 Rust 中初始化字符串数组

java - (Java)尽管链接在浏览器中工作得很好,但 HTTP GET 请求不断收到 400 响应代码