How to Build a Super Simple HTTP proxy in Go in just 20 lines of code

Oct 1, 2023 ยท 3 min read

Go is a great language for writing simple and efficient network applications. Here is how we can build a basic HTTP proxy in Go in under 20 lines of code.

First we import the packages we need:

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

Next we create a HandleProxy func to handle each request:

func HandleProxy(w http.ResponseWriter, r *http.Request) {

  // proxy logic

}

Inside the handler we:

  • Parse the incoming request URL
  • Make the proxied request
  • Copy the response back to client
  • We put this together in a main function to start the proxy server:

    func main() {
    
      http.HandleFunc("/", HandleProxy)
    
      log.Fatal(http.ListenAndServe(":8080", nil))
    
    }
    

    The full code for our 20 line HTTP proxy in Go:

    import (
      "net/http"
      "net/url"
      "io"
      "log"
    )
    
    func HandleProxy(w http.ResponseWriter, r *http.Request) {
    
      dest_url, _ := url.Parse(r.URL.String())
    
      res, _ := http.Get(dest_url.String())
    
      io.Copy(w, res.Body)
    
    }
    
    func main() {
    
      http.HandleFunc("/", HandleProxy)
    
      log.Fatal(http.ListenAndServe(":8080", nil))
    
    }
    

    This is great as a learning exercise but it is easy to see that even the proxy server itself is prone to get blocked as it uses a single IP. In this scenario where you may want a proxy that handles thousands of fetches every day using a professional rotating proxy service to rotate IPs is almost a must.

    Otherwise, you tend to get IP blocked a lot by automatic location, usage, and bot detection algorithms.

    Our rotating proxy server Proxies API provides a simple API that can solve all IP Blocking problems instantly.

  • With millions of high speed rotating proxies located all over the world,
  • With our automatic IP rotation
  • With our automatic User-Agent-String rotation (which simulates requests from different, valid web browsers and web browser versions)
  • With our automatic CAPTCHA solving technology,
  • Hundreds of our customers have successfully solved the headache of IP blocks with a simple API.

    The whole thing can be accessed by a simple API like below in any programming language.

    In fact, you don't even have to take the pain of loading Puppeteer as we render Javascript behind the scenes and you can just get the data and parse it any language like Node, Puppeteer or PHP or using any framework like Scrapy or Nutch. In all these cases you can just call the URL with render support like so:

    curl "<http://api.proxiesapi.com/?key=API_KEY&render=true&url=https://example.com>"
    
    

    We have a running offer of 1000 API calls completely free. Register and get your free API Key.

    Browse by tags:

    Browse by language:

    Tired of getting blocked while scraping the web?

    ProxiesAPI handles headless browsers and rotates proxies for you.
    Get access to 1,000 free API credits, no credit card required!