New Residential Premium, our top-tier pool for the toughest sites
GO

Scraping & code · Setup guide

How to use a proxy in Go (net/http)

Go’s http.Transport takes a proxy URL and sends the username and password from it. No extra packages are needed, even for SOCKS5.

Broken on our side? Fixed in 48h or refunded.

Quick setup

  1. 1Parse your proxy URL with url.Parse.
  2. 2Set Transport: &http.Transport{Proxy: http.ProxyURL(u)}.
  3. 3Turn off keep-alives for a new IP per request.
  4. 4Use socks5:// for SOCKS5, no extra packages.

Your proxy details

After you buy a plan, open My proxies. Choose the Proxy Type (Rotating or Sticky), the Protocol (HTTP or SOCKS5) and a Format, then copy a line. The generator offers three formats:

HOST:PORT:USERNAME:PASSWORD

Paste into apps and anti-detect browsers that split the fields or auto-fill them.

USERNAME:PASSWORD@HOST:PORT

Tools that want credentials before the address.

http://USERNAME:PASSWORD@HOST:PORT

Code and command-line tools. This is the proxy URL used in the examples below.

In the examples on this page, replace USERNAME, PASSWORD, HOST and PORT with the values from your line. SOCKS5_PORT is the port the generator shows when you pick SOCKS5.

Residential lines are either rotating (a new IP on new connections) or sticky (one IP for the time you choose). Datacenter and ISP plans give you a list of dedicated IPs, one per line as IP:PORT:USERNAME:PASSWORD, and each line is one fixed IP.

To test a line, open https://ipinfo.io/json through it: it shows the exit IP and country. For HTTP lines you can also use the free proxy checker.

Basic authenticated proxy

main.go
package main

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

func main() {
	proxyURL, err := url.Parse("http://USERNAME:PASSWORD@HOST:PORT")
	if err != nil {
		panic(err)
	}

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

	resp, err := client.Get("https://ipinfo.io/json")
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(body))
}

The Transport sends the URL’s username and password as Proxy-Authorization, both on the CONNECT request for HTTPS sites and on plain HTTP requests.

Passwords with special characters

Characters such as @, #, / or a space in a password break url.Parse, sometimes without an error. Build the URL from parts instead, and Go encodes the credentials for you.

Go
proxyURL := &url.URL{
	Scheme: "http",
	User:   url.UserPassword("USERNAME", "PASSWORD"),
	Host:   "HOST:PORT",
}

Rotating and sticky IPs

Rotation happens per connection. A rotating line gets a new IP when your client opens a new connection; requests sent through the same open HTTPS tunnel leave from the same IP. A Transport keeps connections alive, so one client can send many requests from one IP. Turn keep-alives off to get a new IP on every request from a rotating line.

To make sticky lines, open the generator, set Proxy Type to Sticky, choose a Sticky Duration if your plan offers one, set how many lines you want in Sticky Amount, then copy or download them. Each line keeps its IP for the chosen time. The session part of a sticky username differs by plan, so always generate sticky lines instead of editing usernames by hand. Use one Transport per sticky line.

Go
transport := &http.Transport{
	Proxy:             http.ProxyURL(proxyURL),
	DisableKeepAlives: true, // a new connection, and a new IP, for every request
}
client := &http.Client{Transport: transport, Timeout: 30 * time.Second}

Target a country

On residential plans, add -country- and a two-letter country code to the end of the username, for example USERNAME-country-us or USERNAME-country-gb. To combine a country with a state, a city or a sticky session, build the line in the generator, which shows only the options your plan supports. Datacenter and ISP IPs have a fixed location that you choose when you buy them.

Go
proxyURL, _ := url.Parse("http://USERNAME-country-us:PASSWORD@HOST:PORT")

SOCKS5

http.ProxyURL accepts SOCKS5 URLs too. You don’t need golang.org/x/net/proxy.

Go
proxyURL, _ := url.Parse("socks5://USERNAME:PASSWORD@HOST:SOCKS5_PORT")
client := &http.Client{
	Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)},
	Timeout:   30 * time.Second,
}

Go sends the hostname to the SOCKS5 proxy, so DNS is resolved at the exit. Recent Go versions also accept socks5h:// and treat it the same as socks5://.

Environment variables

http.DefaultTransport, used by http.Get, reads HTTP_PROXY, HTTPS_PROXY and NO_PROXY through http.ProxyFromEnvironment. Go reads them once, at the first request, so set them before your program starts.

Troubleshooting

  • Get "https://…": Proxy Authentication Required

    The proxy rejected the credentials. Check them, or build the URL with url.UserPassword.More on this error →

  • net/url: invalid userinfo

    The password has characters that aren’t allowed in a URL. Use url.UserPassword.

  • proxyconnect tcp: … connection refused

    Go couldn’t reach the proxy. Check the host and port.More on this error →

  • The IP doesn’t change on a rotating line

    Your client is reusing an open connection. Requests that share a tunnel share its IP. Open a new connection per request, or use one sticky line per task.More on this error →

  • Timeouts

    Set an explicit timeout of 30–60 seconds, retry on a new connection, and lower concurrency.More on this error →

Seeing another error? Read proxy errors and how to fix them, or contact support.

Questions

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

Create &http.Transport{Proxy: http.ProxyURL(u)} with your proxy URL and pass it as the client’s Transport.

Do I need golang.org/x/net/proxy for SOCKS5?

No. net/http supports socks5:// URLs, with username and password, in http.ProxyURL.

Can I pick a different proxy per request?

Yes. Transport.Proxy is a function of the request, so it can return a different URL each time. Keep in mind that connections already open to a proxy are reused.

Does Go read the HTTPS_PROXY environment variable?

Yes, through http.ProxyFromEnvironment, which http.DefaultTransport uses.

Try it with Go for $0.49

Get 1 GB of Residential Lite, then copy your rotating or sticky lines from the generator and paste them into the steps above.

Broken on our side? Fixed in 48h or refunded.

Related guides

All integrations →