I am trying to use a user:pass proxy in my request in golang, and I am getting `proxy authentication required`` when I run it
here is my code:
var ip string = "123.45.67.890:54400:user:pass"
func main() {
proxy := "http://" + ip
proxyURL, err := url.Parse(proxy)
if err != nil {
log.Fatal(err)
}
urlStr := site
url, err := url.Parse(urlStr)
if err != nil {
log.Fatal(err)
}
transport := &http.Transport{
Proxy: http.ProxyURL(proxyURL),
}
jar, _ := cookiejar.New(nil)
client := &http.Client{
Jar: jar,
Transport: transport,
}
req, _ := http.NewRequest("GET", url.String(), nil)
if err != nil {
log.Fatal(err)
}
auth := "johndoe2x:johndoe2x"
basicAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
req.Header.Add("Proxy-Authorization", basicAuth)
req.Header.Add("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
req.Header.Add("accept-language", "en-US,en;q=0.9")
req.Header.Add("upgrade-insecure-requests", "1")
req.Header.Add("user-agent", userAgent)
res, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
log.Println("statusCode:", res.StatusCode)
data, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
log.Println(string(data))
}
Again, when I run this I get Time_here Get "https://site_here/": Proxy Authentication Required
I have been stuck on this for a few hours, and Im not sure exactly how to do it in Go, I was able to do it in JS with a var for user and pass but this one is giving me some trouble in Golang
Thanks in advance!
You are adding the proxy authentication headers to the request which will go to the target server. What you have to do is to add this header to transport
.
Replace req.Header.Add("Proxy-Authorization", basicAuth)
with
transport.ProxyConnectHeader = http.Header{}
transport.ProxyConnectHeader.Add("Proxy-Authorization", basicAuth)