Golang how to use forward http proxy – Best way to configure it? or How to set up a forward HTTP prox

18 Replies, 1544 Views

Had the *exact* same issue last week! For golang how to use forward http proxy, env vars are hit or miss.

Here's a foolproof way:

```go
proxy, err := url.Parse("http://proxy:port")
if err != nil {
log.Fatal(err)
}
client := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxy),
TLSHandshakeTimeout: 10 * time.Second, // avoids hangs
},
}
```

Weird tip: if requests fail, try disabling HTTP/2 (`Transport.ForceAttemptHTTP2 = false`). Proxies can be finicky.

Messages In This Thread



Users browsing this thread: 1 Guest(s)