Fixing 307/308 follow redirects with empty body

dev
mzack 2022-01-02 10:57:35 +01:00
parent faae9672ab
commit 9154dc11bf
1 changed files with 21 additions and 2 deletions

View File

@ -117,8 +117,27 @@ func dump(req *generatedRequest, reqURL string) ([]byte, error) {
if req.request != nil {
// Create a copy on the fly of the request body - ignore errors
bodyBytes, _ := req.request.BodyBytes()
req.request.Request.Body = ioutil.NopCloser(bytes.NewReader(bodyBytes))
return httputil.DumpRequestOut(req.request.Request, true)
if len(bodyBytes) > 0 {
req.request.Request.ContentLength = int64(len(bodyBytes))
req.request.Request.Body = ioutil.NopCloser(bytes.NewReader(bodyBytes))
} else {
req.request.Request.ContentLength = 0
req.request.Request.Body = nil
}
dumpBytes, err := httputil.DumpRequestOut(req.request.Request, true)
if err != nil {
return nil, err
}
// The original req.Body gets modified indirectly by httputil.DumpRequestOut so we set it again to nil if it was empty
// Otherwise redirects like 307/308 would fail (as they require the body to be sent along)
if len(bodyBytes) == 0 {
req.request.Request.ContentLength = int64(len(bodyBytes))
req.request.Request.Body = nil
}
return dumpBytes, nil
}
return rawhttp.DumpRequestRaw(req.rawRequest.Method, reqURL, req.rawRequest.Path, generators.ExpandMapValues(req.rawRequest.Headers), ioutil.NopCloser(strings.NewReader(req.rawRequest.Data)), rawhttp.Options{CustomHeaders: req.rawRequest.UnsafeHeaders, CustomRawBytes: req.rawRequest.UnsafeRawBytes})
}