Skip to content

Commit

Permalink
Web request: combine url params and body params on POST request
Browse files Browse the repository at this point in the history
  • Loading branch information
hoisie committed Jan 25, 2011
1 parent e6bcb7c commit 53009ae
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
23 changes: 16 additions & 7 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,9 @@ func (r *Request) parseParams() (err os.Error) {
return
}
r.FullParams = make(map[string][]string)
var query string
queryParams := r.URL.RawQuery
var bodyParams string
switch r.Method {
case "GET", "HEAD":
query = r.URL.RawQuery
case "POST":
if r.Body == nil {
return os.ErrorString("missing form body")
Expand All @@ -188,7 +187,7 @@ func (r *Request) parseParams() (err os.Error) {
if b, err = ioutil.ReadAll(r.Body); err != nil {
return err
}
query = string(b)
bodyParams = string(b)
case "application/json":
//if we get JSON, do the best we can to convert it to a map[string]string
//we make the body available as r.ParamData
Expand Down Expand Up @@ -241,10 +240,20 @@ func (r *Request) parseParams() (err os.Error) {
return &badStringError{"unknown Content-Type", ct}
}
}
err = parseForm(r.FullParams, query)
if err != nil {
return err
if queryParams != "" {
err = parseForm(r.FullParams, queryParams)
if err != nil {
return err
}
}

if bodyParams != "" {
err = parseForm(r.FullParams, bodyParams)
if err != nil {
return err
}
}

r.Params = flattenParams(r.FullParams)
return nil
}
Expand Down
1 change: 1 addition & 0 deletions web_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ var tests = []Test{
{"POST", "/post/echo/hello", "", 200, "hello"},
{"POST", "/post/echo/hello", "", 200, "hello"},
{"POST", "/post/echoparam/a", "a=hello", 200, "hello"},
{"POST", "/post/echoparam/c?c=hello", "", 200, "hello"},
{"POST", "/post/echoparam/a", "a=hello\x00", 200, "hello\x00"},
//long url
{"GET", "/echo/" + strings.Repeat("0123456789", 100), "", 200, strings.Repeat("0123456789", 100)},
Expand Down

0 comments on commit 53009ae

Please sign in to comment.