UpdateEntryWithRequest populates a HAR entry with values from an HTTP request. It treats the provided body as the body of the HTTP request and does not read or modify r.Body.
(entry *Entry, r *http.Request, body []byte)
| 38 | // UpdateEntryWithRequest populates a HAR entry with values from an HTTP request. It treats the provided |
| 39 | // body as the body of the HTTP request and does not read or modify r.Body. |
| 40 | func UpdateEntryWithRequest(entry *Entry, r *http.Request, body []byte) error { |
| 41 | bodySize := -1 |
| 42 | var postData *PostData |
| 43 | |
| 44 | if body != nil { |
| 45 | bodySize = len(body) |
| 46 | |
| 47 | mimeType := r.Header.Get("Content-Type") |
| 48 | postData = &PostData{ |
| 49 | MimeType: mimeType, |
| 50 | Params: []*Param{}, |
| 51 | Text: string(body), |
| 52 | } |
| 53 | |
| 54 | // ignore missing or malformed mime type here |
| 55 | mediaType, mediaParams, _ := mime.ParseMediaType(mimeType) |
| 56 | |
| 57 | switch mediaType { |
| 58 | case "application/x-www-form-urlencoded": |
| 59 | formdata, err := url.ParseQuery(string(body)) |
| 60 | if err == nil { |
| 61 | for k, v := range formdata { |
| 62 | for _, s := range v { |
| 63 | postData.Params = append(postData.Params, &Param{ |
| 64 | Name: k, |
| 65 | Value: s, |
| 66 | }) |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | case "multipart/form-data": // consider allowing "multipart/mixed" here too |
| 72 | boundary, ok := mediaParams["boundary"] |
| 73 | if !ok { |
| 74 | return fmt.Errorf("got a multipart/form-data request with no boundary in the media type") |
| 75 | } |
| 76 | |
| 77 | mr := multipart.NewReader(bytes.NewReader(body), boundary) |
| 78 | formdata, err := mr.ReadForm(10 * 1024 * 1024) |
| 79 | if err == nil { |
| 80 | for k, v := range formdata.Value { |
| 81 | for _, s := range v { |
| 82 | postData.Params = append(postData.Params, &Param{ |
| 83 | Name: k, |
| 84 | Value: s, |
| 85 | }) |
| 86 | } |
| 87 | } |
| 88 | for k, v := range formdata.File { |
| 89 | for _, s := range v { |
| 90 | postData.Params = append(postData.Params, &Param{ |
| 91 | Name: k, |
| 92 | FileName: s.Filename, |
| 93 | ContentType: s.Header.Get("Content-Type"), |
| 94 | }) |
| 95 | } |
| 96 | } |
| 97 | } |
no test coverage detected