I faced with weird behavior of Do
function in http library.
In my program I have a worker reading a channel. On each message worker calls a function which makes a http request. Here are the function:
func FetchUrlWithProxy(url string, proxy string) (*http.Response, error) {
proxyUrl, err := urllib.Parse(proxy) // [1]
if err != nil {
return nil, err
}
client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}}
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
log.Println("try ", url) // [2]
response, err := client.Do(request)
log.Println("done ", url) // [3]
if err != nil { // [4]
return nil, err
}
return response, nil
}
-
[1]
- it's justnet/url
imported this this name. -
[2]
- this line is printed eveytime. -
[3]
- this line is never printed without any errors/panics. -
[4]
- so this checking of return value is useless. Execution doesn't reach this point.
This behavior definitely is not what I expected. But if I remove proxy usage and create client
as client := &http.Client{}
it will work. On other hand the proxy is not broken. I checked it using curl
. How can it be?
I will provide any additional information if you need.