9951 explained code solutions for 126 technologies


golangHow to use proxy with HTTP client


package main

import ("net/http"; "os"; "io"; "net/url")

func main() {
  proxy, _ := url.Parse("http://host:port")
  h := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxy)}}
  r,_ := h.Get("https://echoof.me");
  defer r.Body.Close()
  io.Copy(os.Stdout, r.Body)
}ctrl + c
package main

default package declaration

net/http

http package to work with http protocol

http.Client{

creates new HTTP client object

Transport

specifies transport object (defines advanced request options)

Proxy:

sets proxy for transport

http://host:port

our proxy host and port

.Get(

sends GET request to the given URL

io.Copy(os.Stdout, r.Body)

output response body to stdout