From fbf3d1729e41ee2ba2db493e45966a973c440990 Mon Sep 17 00:00:00 2001 From: nimrod-up9 <59927337+nimrod-up9@users.noreply.github.com> Date: Mon, 19 Jul 2021 12:38:28 +0300 Subject: [PATCH] Missing request body (#120) * Never use harRequest.PostData.Params. Always use harRequest.PostData.Text. * Comment. --- tap/har_writer.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tap/har_writer.go b/tap/har_writer.go index 3126ee4c0..25ff4f5f9 100644 --- a/tap/har_writer.go +++ b/tap/har_writer.go @@ -1,9 +1,11 @@ package tap import ( + "bytes" "encoding/json" "errors" "fmt" + "io/ioutil" "log" "net/http" "os" @@ -46,12 +48,27 @@ type HarFile struct { } func NewEntry(request *http.Request, requestTime time.Time, response *http.Response, responseTime time.Time) (*har.Entry, error) { - harRequest, err := har.NewRequest(request, true) + harRequest, err := har.NewRequest(request, false) if err != nil { SilentError("convert-request-to-har", "Failed converting request to HAR %s (%v,%+v)", err, err, err) return nil, errors.New("Failed converting request to HAR") } + // For requests with multipart/form-data or application/x-www-form-urlencoded Content-Type, + // martian/har will parse the request body and place the parameters in harRequest.PostData.Params + // instead of harRequest.PostData.Text (as the HAR spec requires it). + // Mizu currently only looks at PostData.Text. Therefore, instead of letting martian/har set the content of + // PostData, always copy the request body to PostData.Text. + if (request.ContentLength > 0) { + reqBody, err := ioutil.ReadAll(request.Body) + if err != nil { + SilentError("read-request-body", "Failed converting request to HAR %s (%v,%+v)", err, err, err) + return nil, errors.New("Failed reading request body") + } + request.Body = ioutil.NopCloser(bytes.NewReader(reqBody)) + harRequest.PostData.Text = string(reqBody) + } + harResponse, err := har.NewResponse(response, true) if err != nil { SilentError("convert-response-to-har", "Failed converting response to HAR %s (%v,%+v)", err, err, err)