handle Body return EOF

handle Body will return EOF when it used in middleware.
This commit is contained in:
2019-03-13 12:11:14 +08:00 committed by GitHub
parent e5261480fd
commit f640a4972c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -28,6 +28,13 @@ func (jsonBinding) Bind(req *http.Request, obj interface{}) error {
if req == nil || req.Body == nil {
return fmt.Errorf("invalid request")
}
// read the body to a variable
bodyBytes, _ := ioutil.ReadAll(req.Body)
req.Body.Close()
tempBody := ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
//reset the body to the original unread state
req.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
return decodeJSON(req.Body, obj)
}