How to setup a proxy with authentication for HTTP client in Golang

  • 22 March 2020
  • ADM

 

How to setup a proxy with authentication for HTTP client in Golang - images/logos/golang.jpg

 

Some proxies will require authentication, with a username and password in order to pass requests. A common scheme is the "basic authentication" where the username and password are concatenated into a string "user:password" and then BASE64 encoded. This is then given to the proxy by the HTTP request header "Proxy-Authorization" with the flag that it is the basic authentication. If you only need to get the BASE64 value you can use this tool.

Here is a Golang example that calls an URL using an HTTP GET with proxy settings and authentication.

Code

package main

import (
	"encoding/base64"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/http/httputil"
	"net/url"
)

func main() {

	//creating the proxyURL
	proxyStr := "http://localhost:7000"
	proxyURL, err := url.Parse(proxyStr)
	if err != nil {
		log.Println(err)
	}

	//creating the URL to be loaded through the proxy
	urlStr := "http://httpbin.org/get"
	url, err := url.Parse(urlStr)
	if err != nil {
		log.Println(err)
	}

	//adding the proxy settings to the Transport object
	transport := &http.Transport{
		Proxy: http.ProxyURL(proxyURL),
	}

	//adding the Transport object to the http Client
	client := &http.Client{
		Transport: transport,
	}

	//generating the HTTP GET request
	request, err := http.NewRequest("GET", url.String(), nil)
	if err != nil {
		log.Println(err)
	}

	//adding proxy authentication
	auth := "user:password"
	basicAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
	request.Header.Add("Proxy-Authorization", basicAuth)

	//printing the request to the console
	dump, _ := httputil.DumpRequest(request, false)
	fmt.Println(string(dump))

	//calling the URL
	response, err := client.Do(request)
	if err != nil {
		log.Println(err)
	}

	log.Println(response.StatusCode)
	log.Println(response.Status)
	//getting the response
	data, err := ioutil.ReadAll(response.Body)
	if err != nil {
		log.Println(err)
	}
	//printing the response
	log.Println(string(data))
}

Compile&Run

To compile the code navigate to the file location and run the following command.

$ go build HTTPClientProxyAuth.go

Then depending if you are on Linux or windows the binary file is created.

To run the application execute the command.

Linux

$ ./HTTPClientProxyAuth

Windows

c:\Users\adm\go\tutorials>HTTPClientProxyAuth.exe

Output

The output will print the response, a JSON string with details about the call.

GET /get HTTP/1.1
Host: httpbin.org
Proxy-Authorization: Basic dXNlcjpwYXNzd29yZA==


2018/02/02 13:12:57 200
2018/02/02 13:12:57 200 OK
2018/02/02 13:12:57 {
 "args": {},
 "headers": {
 "Accept-Encoding": "gzip",
 "Cache-Control": "max-age=259200",
 "Connection": "close",
 "Host": "httpbin.org",
 "User-Agent": "Go-http-client/1.1"
 },
 "origin": "5.93.200.201, 102.222.128.106",
 "url": "http://httpbin.org/get"
}

 

References