get body method into context struct

This commit is contained in:
David Bayo Alcaide 2018-02-16 20:32:41 +01:00
parent 9c90e22b85
commit bdf3366671

View File

@ -5,6 +5,7 @@
package gin
import (
"encoding/json"
"errors"
"io"
"io/ioutil"
@ -56,6 +57,8 @@ type Context struct {
// Accepted defines a list of manually accepted formats for content negotiation.
Accepted []string
body *binding.Body
}
/************************************/
@ -820,3 +823,29 @@ func (c *Context) Value(key interface{}) interface{} {
}
return nil
}
// Body parses body content
//
func (c *Context) Body() (*binding.Body, error) {
if c.body != nil {
return c.body, nil
}
var err error
body := new(binding.Body)
switch c.ContentType() {
case MIMEJSON:
err = json.NewDecoder(c.Request.Body).Decode(&body)
case MIMEPOSTForm:
err = c.Request.ParseForm()
body = binding.ToBody(c.Request.Form)
case MIMEMultipartPOSTForm:
_, err = c.MultipartForm()
body = binding.ToBody(c.Request.Form)
}
c.body = body
return body, err
}