From 3899851f2b38c8a02e3fce600e3539ec8a26e09a Mon Sep 17 00:00:00 2001 From: longshine Date: Fri, 19 May 2023 16:53:12 +0800 Subject: [PATCH] Avoid read all from bytes.Reader when get request body --- client.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/client.go b/client.go index 7f482af..c345af6 100644 --- a/client.go +++ b/client.go @@ -234,14 +234,12 @@ func getBodyReaderAndContentLength(rawBody interface{}) (ReaderFunc, int64, erro // deal with it seeking so want it to match here instead of the // io.ReadSeeker case. case *bytes.Reader: - buf, err := ioutil.ReadAll(body) - if err != nil { - return nil, 0, err - } + snapshot := *body bodyReader = func() (io.Reader, error) { - return bytes.NewReader(buf), nil + r := snapshot + return &r, nil } - contentLength = int64(len(buf)) + contentLength = int64(body.Len()) // Compat case case io.ReadSeeker: