Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Go Httpbin
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Jira
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Container registry
Monitor
Service Desk
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSS
Nix
Go Httpbin
Commits
7ac12a80
Commit
7ac12a80
authored
7 years ago
by
Will McCutchen
Browse files
Options
Downloads
Patches
Plain Diff
Ensure request body is always captured
parent
d4a25987
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
httpbin/handlers_test.go
+1
-3
1 addition, 3 deletions
httpbin/handlers_test.go
httpbin/helpers.go
+19
-14
19 additions, 14 deletions
httpbin/helpers.go
with
20 additions
and
17 deletions
httpbin/handlers_test.go
+
1
−
3
View file @
7ac12a80
...
...
@@ -502,15 +502,13 @@ func TestPost__JSON(t *testing.T) {
t
.
Fatalf
(
"failed to unmarshal body %s from JSON: %s"
,
w
.
Body
,
err
)
}
assertBytesEqual
(
t
,
resp
.
Data
,
inputBody
)
if
len
(
resp
.
Args
)
>
0
{
t
.
Fatalf
(
"expected no query params, got %#v"
,
resp
.
Args
)
}
if
len
(
resp
.
Form
)
!=
0
{
t
.
Fatalf
(
"expected no form values, got %d"
,
len
(
resp
.
Form
))
}
if
resp
.
Data
!=
nil
{
t
.
Fatalf
(
"expected no data, got %#v"
,
resp
.
Data
)
}
// Need to re-marshall just the JSON field from the response in order to
// re-unmarshall it into our expected type
...
...
This diff is collapsed.
Click to expand it.
httpbin/helpers.go
+
19
−
14
View file @
7ac12a80
package
httpbin
import
(
"bytes"
"crypto/sha1"
"encoding/json"
"errors"
...
...
@@ -78,13 +79,25 @@ func parseBody(w http.ResponseWriter, r *http.Request, resp *bodyResponse) error
if
r
.
Body
==
nil
{
return
nil
}
defer
r
.
Body
.
Close
()
// Always set resp.Data to the incoming request body, in case we don't know
// how to handle the content type
body
,
err
:=
ioutil
.
ReadAll
(
r
.
Body
)
if
err
!=
nil
{
r
.
Body
.
Close
()
return
err
}
resp
.
Data
=
body
// After reading the body to populate resp.Data, we need to re-wrap it in
// an io.Reader for further processing below
r
.
Body
.
Close
()
r
.
Body
=
ioutil
.
NopCloser
(
bytes
.
NewBuffer
(
body
))
ct
:=
r
.
Header
.
Get
(
"Content-Type"
)
switch
{
case
ct
==
"application/x-www-form-urlencoded"
:
err
:=
r
.
ParseForm
()
if
err
!=
nil
{
if
err
:=
r
.
ParseForm
();
err
!=
nil
{
return
err
}
resp
.
Form
=
r
.
PostForm
...
...
@@ -92,24 +105,16 @@ func parseBody(w http.ResponseWriter, r *http.Request, resp *bodyResponse) error
// The memory limit here only restricts how many parts will be kept in
// memory before overflowing to disk:
// http://localhost:8080/pkg/net/http/#Request.ParseMultipartForm
err
:=
r
.
ParseMultipartForm
(
1024
*
1024
)
if
err
!=
nil
{
if
err
:=
r
.
ParseMultipartForm
(
1024
);
err
!=
nil
{
return
err
}
resp
.
Form
=
r
.
PostForm
case
strings
.
HasPrefix
(
ct
,
"application/json"
)
:
dec
:=
json
.
NewDecoder
(
r
.
Body
)
err
:=
dec
.
Decode
(
&
resp
.
JSON
)
if
err
!=
nil
{
return
err
}
default
:
data
,
err
:=
ioutil
.
ReadAll
(
r
.
Body
)
if
err
!=
nil
{
if
err
:=
json
.
NewDecoder
(
r
.
Body
)
.
Decode
(
&
resp
.
JSON
);
err
!=
nil
{
return
err
}
resp
.
Data
=
data
}
return
nil
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment