Net / http ignores proxy settings

I use Charles to debug HTTP requests, but it looks like the Go network stack ignores the system proxy settings (in OSX) and the requests are not logged.

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

+5
source share
2 answers

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

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

Then you simply make an http request using the information transport passed from the passed function to the Proxy structure field. Proxy information will be taken from the environment variable $HTTP_PROXY .

+3
source

I just had this exact problem, and the decision I made did NOT solve it for me. This is because my environment variable $HTTP_PROXY not been set!

I managed to solve this problem by setting the environment variables specified 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 custom Transport to my client . He worked with standard transport.

Perhaps because I use a custom shell (zsh), this did not happen automatically. However, the interesting thing is that python would fix Charles Proxy's appearance in the same shell, while Go would not. Updating my .zshrc (or any other shell or profile that you use config) to export the relevant variables.

+3
source

Source: https://habr.com/ru/post/1496577/


All Articles