I read all over the internet, that Go reads HTTP_PROXY environment variable and set proxy for default client. However, it is not working for me and I don't know why.
I'm on Ubuntu 20.04, Go was 1.16, so I upgraded to 1.17 but it still the same.
I have the program below and I execute this in terminal: HTTP_PROXY="http://localhost:8000" go run req.go
I see that first Println prints out correct value, but proxy is not used.
func main() {
fmt.Println(os.Getenv("HTTP_PROXY"))
client := &http.Client{}
resp, err := client.Get("http://localhost:8090/vm/1")
if err != nil {
log.Fatal(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(body))
}
If I modify the code, and set proxy explicitly, it works.
u, err := url.Parse("http://localhost:8000")
if err != nil {
log.Fatal(err)
}
client := &http.Client{
Transport: &http.Transport{Proxy: http.ProxyURL(u)},
}