Skip to content

Commit

Permalink
improve handleFormData
Browse files Browse the repository at this point in the history
Benchmarks:
```
Old: Benchmark_parseRequestBody_FormData-16            954213              1266 ns/op             304 B/op         14 allocs/op
New: Benchmark_parseRequestBody_FormData-16            968466              1248 ns/op             280 B/op         10 allocs/op
```
  • Loading branch information
SVilgelm committed Sep 29, 2023
1 parent 46637d6 commit fe95c1f
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,24 +424,19 @@ func handleMultipart(c *Client, r *Request) (err error) {

func handleFormData(c *Client, r *Request) {
formData := url.Values{}

for k, v := range c.FormData {
for _, iv := range v {
formData.Add(k, iv)
}
}

for k, v := range r.FormData {
// remove form data field from client level by key
// since overrides happens for that key in the request
formData.Del(k)
formData[k] = v[:]
}

for _, iv := range v {
formData.Add(k, iv)
for k, v := range c.FormData {
if _, ok := formData[k]; ok {
continue
}
formData[k] = v[:]
}

r.bodyBuf = bytes.NewBuffer([]byte(formData.Encode()))
r.bodyBuf = acquireBuffer()
r.bodyBuf.WriteString(formData.Encode())
r.Header.Set(hdrContentTypeKey, formContentType)
r.isFormData = true
}
Expand Down

0 comments on commit fe95c1f

Please sign in to comment.