From 341db84788c4d99dd0c38e075bfc65882c9ab204 Mon Sep 17 00:00:00 2001 From: Ringo Hoffmann Date: Fri, 28 Jan 2022 17:06:32 +0100 Subject: [PATCH] close closable body after request --- requests.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/requests.go b/requests.go index 5231c51..1fc00ac 100644 --- a/requests.go +++ b/requests.go @@ -14,10 +14,10 @@ func (c *Client) req(method, path string, body io.Reader, intercept func(*http.R var retryBuf io.Reader if body != nil { - // Because Request#Do closes closable streams, Seeker#Seek - // will fail on retry because stream is already closed. - // This inhibits the closing of the passed stream. - body = closeInhibitor{body} + if cl, ok := body.(io.Closer); ok { + body = closeInhibitor{body} + defer cl.Close() + } // If the authorization fails, we will need to restart reading // from the passed body stream. // When body is seekable, use seek to reset the streams @@ -28,7 +28,6 @@ func (c *Client) req(method, path string, body io.Reader, intercept func(*http.R if _, err = sk.Seek(0, io.SeekStart); err != nil { return } - retryBuf = body } else { buff := &bytes.Buffer{} retryBuf = buff