Skip to content

Commit

Permalink
fix bug of request.Body will be setted to empty if call parseForm twi…
Browse files Browse the repository at this point in the history
…ce (TykTechnologies#2335)

PR TykTechnologies#2182  fix the problem

```go
		var b bytes.Buffer
		r.Body = ioutil.NopCloser(io.TeeReader(r.Body, &b))

		r.ParseForm()

		r.Body = ioutil.NopCloser(&b)

```

But when call the `parseForm` twice at different locations, the `request.Body` will be set to empty.

Because the `r.ParseForm()` will not read the body again(the second time, `r.From` and `r.PostForm` are not `nil`)

so the `io.TeeReader` will not write the body into `var b bytes.Buffer`,  and  `r.Body = ioutil.NopCloser(&b)`, the body will be empty
  • Loading branch information
wklken authored and buger committed Jul 15, 2019
1 parent 43e6886 commit 82f94a5
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion gateway/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ func parseForm(r *http.Request) {
// https://golang.org/pkg/net/http/#Request.ParseForm
// ParseForm drains the request body for a request with Content-Type of
// application/x-www-form-urlencoded
if r.Header.Get("Content-Type") == "application/x-www-form-urlencoded" {
if r.Header.Get("Content-Type") == "application/x-www-form-urlencoded" && r.Form == nil {
var b bytes.Buffer
r.Body = ioutil.NopCloser(io.TeeReader(r.Body, &b))

Expand Down

0 comments on commit 82f94a5

Please sign in to comment.