5

I need to use a proxy with auth using PostForm method. If I use something like (simplified):

request, err := http.NewRequest("GET", url.String(), nil)
response, err := client.Do(request)

I can with ease do request.Header.Add("Proxy-Authorization", basicAuth) and it works fine. But now, I am editing third-party package, and I try to add proxy to the existing code:

    proxyStr := "http://proxy.com:8080"
    proxyURL, _ := url.Parse(proxyStr)

    transport := &http.Transport{
        Proxy: http.ProxyURL(proxyURL),
    }
    bot.Client = &http.Client{
        Transport: transport,
    }

    resp, err := bot.Client.PostForm(method, params)

    auth := "username:password"
    basicAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(auth)) 
    resp.Header.Add("Proxy-Authorization", basicAuth)

It does not work, and it fails, to my mind, at string resp.Header.Add("Proxy-Authorization", basicAuth). Proxy without auth works fine, in this example. Does anybody know, can I use proxy with auth in this case?

1
  • You are adding the authorization header to the response, not to the request. Commented Jul 4, 2018 at 14:50

3 Answers 3

13

You can create the client once by using the following code. Then substitute your HTTP client in the third-party package.

&http.Client{
  Transport: &http.Transport{
    Proxy: http.ProxyURL(&url.URL{
      Scheme: "http",
      User:   url.UserPassword("username", "password"),
      Host:   "146.137.9.45:65233",
    }),
  },
}

or you can parse the URL as well

url, _ := url.Parse("http://username:[email protected]:65233")
&http.Client{
  Transport: &http.Transport{
    Proxy: http.ProxyURL(url),
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

this wont parse... ":" is separator for port
7

You are trying to add a header to a response, which isn't what you send to the server but what you receive. You have to add headers and data to the request, which you have to assemble first and then execute it like this:

data := url.Values{} // the form data
data.Add("foo-key", "some data")
req, err := http.NewRequest("POST","https://yoururl", strings.NewReader(data.Encode()))
auth := "username:password"
basicAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
req.Header.Add("Proxy-Authorization", basicAuth)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := bot.Client.Do(req)

Then you just use the response (resp)

Comments

0

Thanks to all!

I found such a solution (may be it would be useful to someone):

// Uncomment to use proxy with auth
    /*
    proxyStr := "http://proxy.com:3128"
    proxyURL, _ := url.Parse(proxyStr)
    auth := "username:password"
    basicAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
    hdr := http.Header{}
    hdr.Add("Proxy-Authorization", basicAuth)
    transport := &http.Transport{
        Proxy: http.ProxyURL(proxyURL),
        ProxyConnectHeader: hdr,
    }

    bot.Client = &http.Client{
        Transport: transport,
    }
    */

    resp, err := bot.Client.PostForm(method, params)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.