I am trying to retry the request if there is a connection / proxy error. For some reason, I keep getting this error, which does not seem to be recovered, even though attepts are re-requesting 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 http.Request is being consumed anyway, so this is not good on the next try. 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 }
go
Anthony hunt
source share