From 68fc3c05d9c7cb637f62ec3dc3e562d13823724e Mon Sep 17 00:00:00 2001 From: Julien Pivotto Date: Fri, 9 Aug 2024 10:07:27 +0200 Subject: [PATCH] sigv4: support nil body Fixes #562 Fixes #465 Signed-off-by: Julien Pivotto --- sigv4/sigv4.go | 11 +++++++---- sigv4/sigv4_test.go | 7 +++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/sigv4/sigv4.go b/sigv4/sigv4.go index e3e6f052..ae0f76e5 100644 --- a/sigv4/sigv4.go +++ b/sigv4/sigv4.go @@ -110,11 +110,14 @@ func (rt *sigV4RoundTripper) RoundTrip(req *http.Request) (*http.Response, error buf.Reset() rt.pool.Put(buf) }() - if _, err := io.Copy(buf, req.Body); err != nil { - return nil, err + + if req.Body != nil { + if _, err := io.Copy(buf, req.Body); err != nil { + return nil, err + } + // Close the original body since we don't need it anymore. + _ = req.Body.Close() } - // Close the original body since we don't need it anymore. - _ = req.Body.Close() // Ensure our seeker is back at the start of the buffer once we return. var seeker io.ReadSeeker = bytes.NewReader(buf.Bytes()) diff --git a/sigv4/sigv4_test.go b/sigv4/sigv4_test.go index 9e844259..b470672c 100644 --- a/sigv4/sigv4_test.go +++ b/sigv4/sigv4_test.go @@ -107,4 +107,11 @@ func TestSigV4RoundTripper(t *testing.T) { require.Equal(t, "/test/test", gotReq.URL.Path) }) + + t.Run("No body", func(t *testing.T) { + req, err := http.NewRequest(http.MethodGet, "https://example.com/test/test", nil) + require.NoError(t, err) + _, err = cli.Do(req) + require.NoError(t, err) + }) }