Skip to content

Commit 1cc0135

Browse files
committed
fix: populate GetBody to support 307/308 redirects
When sending POST requests with a non-empty body that trigger 307/308 redirects, the redirects fail because the underlying `http.Request.GetBody` is nil by default. This patch populates `GetBody` using the existing reusable body reader, allowing the `net/http` client to rewind and replay the body. Fixes #493. Signed-off-by: Dwi Siswanto <[email protected]>
1 parent e49953b commit 1cc0135

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

request.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7+
"io"
78
"net/http"
89
"net/http/httptrace"
910
"net/http/httputil"
@@ -197,6 +198,9 @@ func FromRequest(r *http.Request) (*Request, error) {
197198
if err != nil {
198199
return nil, err
199200
}
201+
r.GetBody = func() (io.ReadCloser, error) {
202+
return body, nil
203+
}
200204
}
201205

202206
return &req, nil
@@ -259,6 +263,9 @@ func NewRequestFromURLWithContext(ctx context.Context, method string, urlx *urlu
259263
if bodyReader != nil {
260264
httpReq.ContentLength = contentLength
261265
httpReq.Body = bodyReader
266+
httpReq.GetBody = func() (io.ReadCloser, error) {
267+
return bodyReader, nil
268+
}
262269
}
263270

264271
request := &Request{

0 commit comments

Comments
 (0)