Tutorials 4 min read

Go HTTP Client Proxy: net/http Transport, Auth and SOCKS5 (2026)

Go's standard library handles authenticated HTTP and SOCKS5 proxies without extra packages. The details are in the Transport.

Setting a Go HTTP client proxy needs nothing beyond the standard library. net/http reads the proxy from Transport.Proxy, sends Proxy-Authorization from the URL's user info, and supports socks5:// URLs natively. The things that trip people up are elsewhere: sharing one transport across sessions that should have different IPs, forgetting timeouts, and special characters in passwords.

The Minimal Go Proxy Client

package main

import (
	"fmt"
	"io"
	"net/http"
	"net/url"
	"time"
)

func main() {
	proxyURL, err := url.Parse("http://USERNAME-country-us:[email protected]:8080")
	if err != nil {
		panic(err)
	}

	client := &http.Client{
		Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)},
		Timeout:   30 * time.Second,
	}

	resp, err := client.Get("https://api.ipify.org")
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()
	body, _ := io.ReadAll(resp.Body)
	fmt.Println(resp.StatusCode, string(body))
}

For https URLs Go opens a CONNECT tunnel and sends the credentials to the proxy; the TLS session runs end to end between your program and the site.

Building the Proxy URL Safely

If the password contains characters like @, : or #, string concatenation produces a broken URL. Build it with url.UserPassword, which escapes them for you:

func proxyURL(username, password string) *url.URL {
	return &url.URL{
		Scheme: "http",
		User:   url.UserPassword(username, password),
		Host:   "global.nodeproxies.xyz:8080",
	}
}

Using Environment Variables Instead

The default transport uses http.ProxyFromEnvironment, which reads HTTP_PROXY, HTTPS_PROXY and NO_PROXY. That's convenient for CLI tools:

export HTTPS_PROXY='http://USERNAME:[email protected]:8080'
go run main.go

With env vars, http.Get and http.DefaultClient use the proxy without any code. Note that the environment is read once per process and cached, so changing it at runtime has no effect.

SOCKS5 in Go

http.ProxyURL accepts socks5:// URLs, with credentials:

proxyURL, _ := url.Parse("socks5://USERNAME:[email protected]:1080")
transport := &http.Transport{Proxy: http.ProxyURL(proxyURL)}

For non-HTTP protocols, such as raw TCP connections, use golang.org/x/net/proxy to get a Dialer. For plain web requests, the HTTP endpoint and SOCKS5 behave the same; see SOCKS5 vs HTTP proxies.

One Transport per Sticky Session

http.Transport keeps connections alive and reuses them. That matters with rotating proxies: a reused CONNECT tunnel keeps the same exit IP, so even without a session parameter, requests on a kept-alive connection may not rotate. Two practical rules follow:

  • For a stable IP, give each logical session its own client with a sticky username, for example USERNAME-country-us-session-job42-time-10.
  • For an IP per request, set DisableKeepAlives: true on the transport, so each request opens a fresh tunnel and gets a new exit.
func newSessionClient(sessionID string) *http.Client {
	u := proxyURL("USERNAME-country-us-session-"+sessionID+"-time-10", "PASSWORD")
	return &http.Client{
		Transport: &http.Transport{
			Proxy:               http.ProxyURL(u),
			MaxIdleConnsPerHost: 4,
			IdleConnTimeout:     60 * time.Second,
		},
		Timeout: 30 * time.Second,
	}
}

Don't create a new transport per request in a loop with keep-alives on, though; idle connections pile up. Either reuse the session client or disable keep-alives. More on choosing between the two in rotating vs sticky proxies.

Handling Proxy Errors in Go

When the proxy refuses a CONNECT, Go returns an error rather than a response, with the proxy's status in the message, for example "Proxy Authentication Required". Check for it so you don't retry pointlessly:

if err != nil {
	msg := err.Error()
	switch {
	case strings.Contains(msg, "Proxy Authentication Required"):
		log.Fatal("407: check username/password and that the plan is active")
	case strings.Contains(msg, "Payment Required"):
		log.Fatal("402: plan is out of bandwidth")
	default:
		// timeouts, resets, 502/503: retry with backoff
	}
}

Add strings and log to the imports. For plain http:// targets, the proxy's status comes back as resp.StatusCode instead, so check both paths. Why proxies get blocked covers the target-side failures.

Different Proxies per Destination

Transport.Proxy is a function, so you can decide per request. This sends internal hosts direct and everything else through the proxy:

proxy := proxyURL("USERNAME-country-us", "PASSWORD")

transport := &http.Transport{
	Proxy: func(r *http.Request) (*url.URL, error) {
		switch r.URL.Hostname() {
		case "localhost", "internal.example.com":
			return nil, nil // direct
		}
		return proxy, nil
	},
}

Returning nil, nil means no proxy. The same pattern lets you route different targets through different countries, by returning a different URL depending on the host. Keep the URLs pre-built rather than parsing on every request.

Concurrency Without Getting Blocked

Goroutines make it trivial to launch thousands of requests at once, which is exactly what gets you rate-limited. Bound concurrency with a buffered channel used as a semaphore, for example sem := make(chan struct{}, 20), acquiring before each request and releasing after. Twenty in flight through rotating IPs is plenty for most targets.

Using Go with NodeProxies

Point Transport.Proxy at http://global.nodeproxies.xyz:8080 or socks5://global.nodeproxies.xyz:1080 and set options in the username. The proxy username builder generates the string. You can also manage proxies programmatically through the API described in the docs. Sign up with a $3 minimum deposit.

FAQ

How do I set a proxy for http.Client in Go?

Create an http.Transport with Proxy: http.ProxyURL(u) and use it as the client's Transport.

Does Go support proxy authentication?

Yes. Put the username and password in the proxy URL and Go sends the Proxy-Authorization header automatically.

Why isn't my Go client rotating IPs?

Keep-alive reuses the same tunnel. Set DisableKeepAlives: true for a new IP per request, or use explicit session ids.

NodeProxies

Ready to get started?

90M+ residential, mobile & datacenter proxies. Datacenter from $0.30/GB, residential from $0.42/GB. No contracts.

Create free account