Skip to content

Commit

Permalink
net/http: query values from r.PostForm first in *Request.FormValue
Browse files Browse the repository at this point in the history
As the doc of http.Request.FormValue says:
"POST, PUT, and PATCH body parameters take precedence over URL query string
values".

So query values from r.PostForm first.

Fixes #64575
  • Loading branch information
callthingsoff committed Dec 6, 2023
1 parent 01dfae9 commit c64eb23
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/net/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,9 @@ func (r *Request) FormValue(key string) string {
if r.Form == nil {
r.ParseMultipartForm(defaultMaxMemory)
}
if vs := r.PostForm[key]; len(vs) > 0 {
return vs[0]
}
if vs := r.Form[key]; len(vs) > 0 {
return vs[0]
}
Expand Down
7 changes: 5 additions & 2 deletions src/net/http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func TestParseFormSemicolonSeparator(t *testing.T) {
}

func TestParseFormQuery(t *testing.T) {
req, _ := NewRequest("POST", "http://www.google.com/search?q=foo&q=bar&both=x&prio=1&orphan=nope&empty=not",
strings.NewReader("z=post&both=y&prio=2&=nokey&orphan&empty=&"))
req, _ := NewRequest("POST", "http://www.google.com/search?q=foo&q=bar&both=x&prio=1&orphan=nope&empty=not&dup=url",
strings.NewReader("z=post&both=y&prio=2&=nokey&orphan&empty=&dup=body&"))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")

if q := req.FormValue("q"); q != "foo" {
Expand All @@ -77,6 +77,9 @@ func TestParseFormQuery(t *testing.T) {
if prio := req.FormValue("prio"); prio != "2" {
t.Errorf(`req.FormValue("prio") = %q, want "2" (from body)`, prio)
}
if dup := req.FormValue("dup"); dup != "body" {
t.Errorf(`req.FormValue("dup") = %q, want "body" (from body)`, dup)
}
if orphan := req.Form["orphan"]; !reflect.DeepEqual(orphan, []string{"", "nope"}) {
t.Errorf(`req.FormValue("orphan") = %q, want "" (from body)`, orphan)
}
Expand Down

0 comments on commit c64eb23

Please sign in to comment.