3

I have env variable set for http_proxy, but with another call, I like to bypass proxy and use direct connection to destination server instead.

Is there any way I can do that in Go lang?

Thanks.

2
  • Just don't use the http.DefaultTransport: See golang.org/pkg/net/http/#RoundTripper Commented May 31, 2017 at 17:49
  • awesome thanks Volker. Commented May 31, 2017 at 21:10

3 Answers 3

3

As @Volker mentioned, you can either:

  • Use your own RoundTripper instead of the DefaultTransport
  • Modify DefaultTransport.Proxy to return nil for the request in question
  • If the call you want to ignore proxies with is to a specific host, and you always want to ignore proxy for calls to that host, add the host to the NO_PROXY environment variable
Sign up to request clarification or add additional context in comments.

Comments

3

this is what I did:

var defaultTransport http.RoundTripper = &http.Transport{
    Proxy: nil,
    DialContext: (&net.Dialer{
        Timeout:   10 * time.Second,
        KeepAlive: 30 * time.Second,
        DualStack: true,
    }).DialContext,
    MaxIdleConns:          30,
    IdleConnTimeout:       90 * time.Second,
    TLSHandshakeTimeout:   15 * time.Second,
    ExpectContinueTimeout: 1 * time.Second,
}

client := &http.Client{Transport: defaultTransport}

Comments

3

I had that problem with a corporate proxy, even disabled the proxy url it was using it this was my solution

// reset proxy in http RoundTripper
var defaultTransport http.RoundTripper = &http.Transport{Proxy: nil}
client := &http.Client{Transport: defaultTransport}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.