Skip to content

Commit

Permalink
ref: ReadJSON now destroys the body of the request
Browse files Browse the repository at this point in the history
  • Loading branch information
noxecane committed Sep 12, 2020
1 parent a410175 commit c7d0c8f
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/mitchellh/mapstructure"
)

// ReadBody extracts the bytes in a request body without destroying the contents of the body
// ReadBody extracts the bytes in a request body without destroying the contents of the body
func ReadBody(r *http.Request) []byte {
var buffer bytes.Buffer

Expand All @@ -33,8 +33,8 @@ func ReadBody(r *http.Request) []byte {
return body
}

// ReadJSON decodes the JSON body of the request without destroying the request and
// validates it. If the content type is not JSON it fails with a 415. Otherwise it fails
// ReadJSON decodes the JSON body of the request and destroys to prevent possible issues with
// writing a response. If the content type is not JSON it fails with a 415. Otherwise it fails
// with a 400 on validation errors.
func ReadJSON(r *http.Request, v interface{}) {
// make sure we are reading a JSON type
Expand All @@ -46,14 +46,7 @@ func ReadJSON(r *http.Request, v interface{}) {
})
}

// copy request body to in memory buffer while being read
var buffer bytes.Buffer
bodyReader := io.TeeReader(r.Body, &buffer)

// make sure others can read the body
r.Body = ioutil.NopCloser(&buffer)

err := json.NewDecoder(bodyReader).Decode(v)
err := json.NewDecoder(r.Body).Decode(v)
switch {
case err == io.EOF:
// tell the user all the required attributes
Expand Down Expand Up @@ -85,6 +78,8 @@ func ReadJSON(r *http.Request, v interface{}) {
}
}

// ReadQuery reads the requests URL query parameters into a struct.
// It doesn't support multi-value parameters
func ReadQuery(r *http.Request, v interface{}) {
raw := r.URL.Query()
qMap := make(map[string]string)
Expand Down

0 comments on commit c7d0c8f

Please sign in to comment.