diff --git a/client/httplib/client_option_test.go b/client/httplib/client_option_test.go index 28cbc6fe..79e5103f 100644 --- a/client/httplib/client_option_test.go +++ b/client/httplib/client_option_test.go @@ -247,7 +247,7 @@ func TestOption_WithRetry(t *testing.T) { } retryAmount := 1 - retryDelay := 200 * time.Millisecond + retryDelay := 800 * time.Millisecond startTime := time.Now().UnixNano() / int64(time.Millisecond) _ = client.Get(nil, "", WithRetry(retryAmount, retryDelay)) diff --git a/client/httplib/httpclient.go b/client/httplib/httpclient.go index ef2e27eb..c2a61fcf 100644 --- a/client/httplib/httpclient.go +++ b/client/httplib/httpclient.go @@ -80,7 +80,13 @@ func (c *Client) customReq(req *BeegoHTTPRequest, opts []BeegoHTTPRequestOption) // handleResponse try to parse body to meaningful value func (c *Client) handleResponse(value interface{}, req *BeegoHTTPRequest) error { - err := c.handleCarrier(value, req) + // make sure req.resp is not nil + _, err := req.Bytes() + if err != nil { + return err + } + + err = c.handleCarrier(value, req) if err != nil { return err } @@ -90,22 +96,17 @@ func (c *Client) handleResponse(value interface{}, req *BeegoHTTPRequest) error // handleCarrier set http data to value func (c *Client) handleCarrier(value interface{}, req *BeegoHTTPRequest) error { - resp, err := req.Response() - if err != nil { - return err - } - defer resp.Body.Close() - if value == nil { - return err + return nil } + if carrier, ok := value.(HTTPResponseCarrier); ok { b, err := req.Bytes() if err != nil { return err } - resp.Body = ioutil.NopCloser(bytes.NewReader(b)) - carrier.SetHTTPResponse(resp) + req.resp.Body = ioutil.NopCloser(bytes.NewReader(b)) + carrier.SetHTTPResponse(req.resp) } if carrier, ok := value.(HTTPBodyCarrier); ok { b, err := req.Bytes() @@ -123,10 +124,10 @@ func (c *Client) handleCarrier(value interface{}, req *BeegoHTTPRequest) error { carrier.SetBytes(b) } if carrier, ok := value.(HTTPStatusCarrier); ok { - carrier.SetStatusCode(resp.StatusCode) + carrier.SetStatusCode(req.resp.StatusCode) } if carrier, ok := value.(HTTPHeadersCarrier); ok { - carrier.SetHeader(resp.Header) + carrier.SetHeader(req.resp.Header) } return nil } diff --git a/client/httplib/httplib.go b/client/httplib/httplib.go index 76583712..b102f687 100644 --- a/client/httplib/httplib.go +++ b/client/httplib/httplib.go @@ -662,13 +662,7 @@ func (b *BeegoHTTPRequest) ToValue(value interface{}) error { return nil } - resp, err := b.Response() - if err != nil { - return err - } - defer resp.Body.Close() - - contentType := strings.Split(resp.Header.Get(contentTypeKey), ";")[0] + contentType := strings.Split(b.resp.Header.Get(contentTypeKey), ";")[0] // try to parse it as content type switch contentType { case "application/json":