I'm trying to retry a request if there is a connection/proxy error. For some reasons I keep getting this error which doesn't seem to recover regardless the attepts to retry the request:
Post https://m.somewebsite.co.uk/api/di/34433: http: ContentLength=222 with Body length 0
Am I doing something wrong? My first suspicion is that the http.Request is consumed somehow so on the next attempts it's no longer good. Should I manage a copy?
func Post(URL string, form url.Values, cl *http.Client) ([]byte, error) {
req, err := http.NewRequest("POST", URL, strings.NewReader(form.Encode()))
if err != nil {
log.Error(err)
return nil, err
}
req.Header.Set("User-Agent", ua)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
rsp, err := do(cl, req)
if err != nil {
return nil, err
}
defer rsp.Body.Close()
b, err := ioutil.ReadAll(rsp.Body)
if err != nil {
log.Error(err)
return nil, err
}
return b, nil
}
func do(cl *http.Client, req *http.Request)(*http.Response, error){
rsp, err := cl.Do(req)
for i := 0; IsErrProxy(err); i++ {
log.Errorf("Proxy is slow or down ")
time.Sleep(6 * time.Second)
5t rsp, err = cl.Do(&ncp)
if err == nil{
return rsp, nil
}
if i > 10 {
return nil, fmt.Errorf("after %v tries error: %v", i, err)
}
}
return rsp, err
}