From 5268213683f7904bd8cb17e1db32d8a2e1a2e862 Mon Sep 17 00:00:00 2001 From: Charlie Crawford Date: Thu, 4 Aug 2022 08:29:58 -0400 Subject: [PATCH] Close response closer to the other close scenario. --- client.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index c53cc2c1..737ec776 100644 --- a/client.go +++ b/client.go @@ -581,7 +581,6 @@ func (c *Client) checkResponse(resp *http.Response, err error) (*http.Response, } if resp.StatusCode < 200 || resp.StatusCode > 299 { - defer func() { _ = resp.Body.Close() }() return resp, c.getErrorFromResponse(resp) } @@ -591,6 +590,8 @@ func (c *Client) checkResponse(resp *http.Response, err error) (*http.Response, func (c *Client) getErrorFromResponse(resp *http.Response) APIError { // check whether the error response is declared as JSON if !strings.HasPrefix(resp.Header.Get("Content-Type"), "application/json") { + defer func() { _ = resp.Body.Close() }() + aerr := APIError{ StatusCode: resp.StatusCode, message: fmt.Sprintf("HTTP response with status code %d does not contain Content-Type: application/json", resp.StatusCode),