在重定向和代理上转到http.Request标头

https://groups.google.com/forum/#!topic/golang-nuts/OwGvopYXpwE

As seen in this thread, when http.Client sends requests to redirects, the header gets reset.

There is a workaround like:

client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
    if len(via) >= 10 {
        return fmt.Errorf("too many redirects")
    }
    if len(via) == 0 {
        return nil
    }
    for attr, val := range via[0].Header {
        if _, ok := req.Header[attr]; !ok {
            req.Header[attr] = val
        }
    }
    return nil
}

But my question is how do I do this if I want to http Request through my proxy server.

When http request goes through proxy server, does header get all reset? Do I have to set up another http.Client in proxy?

I set up my proxy server using https://github.com/elazarl/goproxy

Thanks,

I looked briefly at the code, at it looks like goproxy doesn't handle 3XX responses in any special way – they're simply routed back to your client, so it can react accordingly. In your case, you'll issue another request with all the headers set.

If it wasn't your concern and you were just wondering if simply existence of proxy requires any hack on the client side, then no, http.Client won't remove any headers when proxy is in use, it won't even know there's a proxy involved.

You can easily test these assumption by setting up you own a simple server (in Go for example) which is returning a redirect response (to itself or some nc listener) and printing all the headers. When routing the client to the server through your proxy, you can make sure everything looks good on the server side.