Skip to content

Instantly share code, notes, and snippets.

@kemadz
Last active March 19, 2025 03:23
Show Gist options
  • Save kemadz/12a8e60a48ea995bfced4f64751d2881 to your computer and use it in GitHub Desktop.
Save kemadz/12a8e60a48ea995bfced4f64751d2881 to your computer and use it in GitHub Desktop.
golang http client proxy sample code
package main
import (
"io"
"log"
"net/http"
)
func main() {
// url := "http://httpbin.org/user-agent"
url := "https://ip.im/"
// proxyURL, _ := url.Parse("http://127.0.0.1:7890")
// proxy := http.ProxyURL(proxyURL)
// transport := &http.Transport{Proxy: proxy}
transport := &http.Transport{Proxy: http.ProxyFromEnvironment}
client := &http.Client{Transport: transport}
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("User-Agent", "curl/8.12.1")
resp, _ := client.Do(req)
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
log.Println(http.ProxyFromEnvironment(req))
log.Println(string(body))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment