From 42acf3442b67b4ff5ad601e249c55127992bcf3f Mon Sep 17 00:00:00 2001 From: Steve Coffman Date: Wed, 25 Aug 2021 17:59:04 -0400 Subject: [PATCH] fix: Avoid clobbering body so subsequent reads like middleware will not get nil Signed-off-by: Steve Coffman --- http2curl.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/http2curl.go b/http2curl.go index bde4d44..74c6d99 100644 --- a/http2curl.go +++ b/http2curl.go @@ -3,6 +3,7 @@ package http2curl import ( "bytes" "fmt" + "io/ioutil" "net/http" "sort" "strings" @@ -37,8 +38,10 @@ func GetCurlCommand(req *http.Request) (*CurlCommand, error) { var buff bytes.Buffer _, err := buff.ReadFrom(req.Body) if err != nil { - return nil, fmt.Errorf("getCurlCommand: buffer read from body erorr: %w", err) + return nil, fmt.Errorf("getCurlCommand: buffer read from body error: %w", err) } + // reset body for potential re-reads + req.Body = ioutil.NopCloser(bytes.NewBuffer(buff.Bytes())) if len(buff.String()) > 0 { bodyEscaped := bashEscape(buff.String()) command.append("-d", bodyEscaped) @@ -60,3 +63,4 @@ func GetCurlCommand(req *http.Request) (*CurlCommand, error) { return &command, nil } +