diff --git a/conjure-go-client/httpclient/body_handler.go b/conjure-go-client/httpclient/body_handler.go index 57def3ef..7835baa9 100644 --- a/conjure-go-client/httpclient/body_handler.go +++ b/conjure-go-client/httpclient/body_handler.go @@ -16,6 +16,7 @@ package httpclient import ( "bytes" + "github.com/palantir/conjure-go-runtime/v2/conjure-go-client/httpclient/internal" "io" "io/ioutil" "net/http" @@ -104,6 +105,9 @@ func (b *bodyMiddleware) readResponse(resp *http.Response, respErr error) error if b.rawOutput && respErr == nil { return nil } + defer func() { + internal.DrainBody(resp) + }() if respErr != nil { return respErr @@ -112,6 +116,7 @@ func (b *bodyMiddleware) readResponse(resp *http.Response, respErr error) error // Verify we have a body to unmarshal. If the request was unsuccessful, the errorMiddleware will // set a non-nil error and return no response. if b.responseOutput == nil || resp == nil || resp.Body == nil || resp.ContentLength == 0 { + internal.DrainBody(resp) return nil } @@ -119,6 +124,5 @@ func (b *bodyMiddleware) readResponse(resp *http.Response, respErr error) error if decErr != nil { return decErr } - return nil } diff --git a/conjure-go-client/httpclient/client.go b/conjure-go-client/httpclient/client.go index 90aaf41f..7aa4ab32 100644 --- a/conjure-go-client/httpclient/client.go +++ b/conjure-go-client/httpclient/client.go @@ -114,11 +114,6 @@ func (c *clientImpl) Do(ctx context.Context, params ...RequestParam) (*http.Resp reqCopy := req.Clone(ctx) resp, err = clientCopy.Do(reqCopy) err = unwrapURLError(ctx, err) - // unless this is exactly the scenario where the caller has opted into being responsible for draining and closing - // the response body, be sure to do so here. - if !(err == nil && b.bodyMiddleware.rawOutput) { - internal.DrainBody(resp) - } attempts++ if resp != nil && isSuccessfulOrBadRequest(resp.StatusCode) { break