PiratProxies

Web scraping

Proxies in Go net/http: working examples

Go's standard HTTP client takes a proxy on its transport. The username and password are read from the proxy address.

The right proxy type for Go net/http

For most scraping, Residential Lite is the cheapest way to get home IP addresses in the right country. Use Residential when you need a particular state or city, and Unlimited Datacenter for targets in the United States that do not block data centre addresses.

The values to enter in Go net/http

Every PiratProxies plan comes down to four values: host, port, username and password. Here they are for Residential; your own username and password are on your plan page.

Hostresi.piratproxies.com
HTTP port8080
SOCKS5 port1080
UsernameUSERNAME
Passwordshown on your plan page

Written as a single address:

http://USERNAME:PASSWORD@resi.piratproxies.com:8080

There is no separate country or session field: both are part of the username. Your plan page has a generator that writes it, and Targeting and sessions describes each piece.

Setting it up in Go net/http

  1. Parse the proxy address with url.Parse.
  2. Set Proxy: http.ProxyURL(...) on the transport and use that client.

Code: Go net/http through a proxy

Go

package main

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

func main() {
	proxy, _ := url.Parse("http://USERNAME:PASSWORD@resi.piratproxies.com:8080")
	client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxy)}}
	res, err := client.Get("https://api.ipify.org")
	if err != nil {
		panic(err)
	}
	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)
	fmt.Println(string(body))
}

Put your own login from the plan page in place of USERNAME and PASSWORD. The example calls an IP-check service, so what it prints is the address your request came from.

Should Go net/http rotate IPs or keep one?

When Go net/http is fetching lots of unrelated pages, leave the session out of the username and each request gets a fresh IP address. When requests belong together, such as a login, a basket or paginated results that set cookies, put a session in the username so they all share one address.

Here is what a sticky username looks like:

USERNAME-country-us-time-600-session-a1b2c3d4

When Go net/http will not connect

  • The transport keeps connections alive, so repeated requests can share one IP address. Set DisableKeepAlives: true when you want a new address for every request.
  • A socks5:// address works in the same place, with port 1080.
  • Error 407 means the login was refused. Copy the username and password from your plan page once more, and make sure no space crept in and the username was not cut short.
  • An IP in the wrong country nearly always comes from the username, since the country code lives there. Other fixes.

Go net/http and proxies: questions

Can Go net/http use a SOCKS5 proxy?

Yes. Point it at port 1080 with the same username and password.

Which PiratProxies plan suits Go net/http best?

For most scraping, Residential Lite is the cheapest way to get home IP addresses in the right country. Use Residential when you need a particular state or city, and Unlimited Datacenter for targets in the United States that do not block data centre addresses.

Words used in this guide: web scraping, rotating proxy, sticky session, rate limiting, headless browser.

Go net/http belongs to its own developers, and PiratProxies has no connection with them; we name it only to say what this guide covers. Follow the rules of the sites you visit and our acceptable use policy.