Making a Go HTTP client proxy aware

by Hadley Bradley


One of my web applications was recently installed at a county council and my HTTP client code which called an API endpoint was failing. It was failing because the council used proxy servers and my code wasn’t proxy aware. It turned out to be an easy fix. I just needed to check for the presence of an environment variable called HTTP_PROXY. If the environment variable is present, then we parse the value using the url standard library to check that the format is valid.

If the parsed proxy url is valid, then we can pass it into the Proxy struct of the http.Transport method.

// define a HTTP client with a sensible
// timeout value for the API service
// being called by this client
var migClient = &http.Client{
    Timeout: 10 * time.Second,

// check if the proxy environment
// variable has been set
if len(os.Getenv("HTTPS_PROXY")) > 0 {

    proxyURL, err := url.Parse(os.Getenv("HTTPS_PROXY"))
    if err != nil {
        log.Println("WARN: ERROR PARSING PROXY : " + err.Error())
    } else {
        migClient.Transport = &http.Transport{
            TLSClientConfig:       cfg,
            IdleConnTimeout:       10 * time.Second,
            ResponseHeaderTimeout: 10 * time.Second,
            Proxy:                 http.ProxyURL(proxyURL),

If your only writing software for your company, where you control the infrastructure this is probably not needed. However, if your writing software that will be used in organisations where you don’t control the server stack; then take the extra care to ensure your HTTP clients are proxy aware.