From e23dfbec09e0d92a6fc4bb5ed7d241e9eb05690e Mon Sep 17 00:00:00 2001 From: childe Date: Tue, 6 Jul 2021 17:35:13 +0800 Subject: [PATCH] fix: empty http request body in Connection.Call RequestOpts.Body is read out in the first try, and will be empty in the second retry. --- swift.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/swift.go b/swift.go index 53b3112f5..ee30d1add 100644 --- a/swift.go +++ b/swift.go @@ -714,6 +714,11 @@ func (c *Connection) Call(ctx context.Context, targetUrl string, p RequestOpts) retries = c.Retries } var req *http.Request + reqBodyBuf, err := ioutil.ReadAll(p.Body) + if err != nil { + return + } + reader := bytes.NewReader(reqBodyBuf) for { var authToken string if targetUrl, authToken, err = c.getUrlAndAuthToken(ctx, targetUrl, p.OnReAuth); err != nil { @@ -735,11 +740,9 @@ func (c *Connection) Call(ctx context.Context, targetUrl string, p RequestOpts) } timer := time.NewTimer(c.ConnectTimeout) defer timer.Stop() - reader := p.Body - if reader != nil { - reader = newWatchdogReader(reader, c.Timeout, timer) - } - req, err = http.NewRequestWithContext(ctx, p.Operation, URL.String(), reader) + reader.Reset(reqBodyBuf) // or reader.Seek(0, io.SeekStart) ? + watchdogReader := newWatchdogReader(reader, c.Timeout, timer) + req, err = http.NewRequestWithContext(ctx, p.Operation, URL.String(), watchdogReader) if err != nil { return }