Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

net/http ignoring system proxy settings

Tags:

go

I'm using Charles to debug HTTP requests, but it seems that Go's network stack ignores the system proxy settings (on OSX) and the requests are not logged.

How do I tell Go that the requests should use the proxy?

like image 921
Era Avatar asked Aug 12 '13 12:08

Era


2 Answers

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.

like image 76
Benjamin Sussman Avatar answered Sep 21 '22 00:09

Benjamin Sussman


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.

like image 30
Rostyslav Dzinko Avatar answered Sep 22 '22 00:09

Rostyslav Dzinko