net/http ignoring system proxy settings

I just had this exact issue, and the accepted solution did NOT solve it for me. That's because my $HTTP_PROXY environment variable was not set!

I was able to solve it by setting up my environment variables as per indicated here: http://www.bonusbits.com/wiki/HowTo:Setup_Charles_Proxy_on_Mac Then once the variable was set correctly, I didn't even need to apply a custom Transport to my client. It worked with the default transport.

Perhaps because I'm using a custom shell (zsh) this didn't happen automatically. However what's interesting is that python would correct appear in Charles Proxy in the same shell while Go would not. Updating my .zshrc (or whatever shell or profile you are using's config) to export the appropriate variables worked.


You can get proxy info using ProxyFromEnvironment function. Then you create http client using transport (represented by RoundTripper interface) that has info about your proxy settings:

var PTransport http.RoundTripper = &http.Transport{Proxy: http.ProxyFromEnvironment}

client := http.Client{Transport: PTransport}

Then you just do http request using the info transport gets from passed function to Proxy struct field. Proxy info will be taken from $HTTP_PROXY environment variable.

Tags:

Go