I'm trying to setup the HTTP client so that it uses a proxy, however I cannot quite understand how to do it. The documentation has multiple reference to "proxy" but none of the functions seem to allow to define the proxy. What I need is something like this:
client := &http.Client{}
client.SetProxy("someip:someport") // pseudo code
resp, err := client.Get("http://example.com") // do request through proxy
Any idea how to do this in Go?
Go will use the the proxy defined in the environment variable
HTTP_PROXY
if it's set. Otherwise it will use no proxy.You could do it like this:
May you could also try this:
For an alternative way, you can also use GoRequest which has a feature that you can set proxy easily for any single request.
Or you can set for the whole at once.
lukad is correct, you could set the
HTTP_PROXY
environment variable, if you do this Go will use it by default.Bash:
Go:
You could also construct your own http.Client that MUST use a proxy regardless of the environment's configuration:
This is useful if you can not depend on the environment's configuration, or do not want to modify it.
You could also modify the default transport used by the "
net/http
" package. This would affect your entire program (including the default HTTP client).