mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-22 17:42:14 +08:00
get body method into context struct
This commit is contained in:
parent
9c90e22b85
commit
bdf3366671
29
context.go
29
context.go
@ -5,6 +5,7 @@
|
|||||||
package gin
|
package gin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@ -56,6 +57,8 @@ type Context struct {
|
|||||||
|
|
||||||
// Accepted defines a list of manually accepted formats for content negotiation.
|
// Accepted defines a list of manually accepted formats for content negotiation.
|
||||||
Accepted []string
|
Accepted []string
|
||||||
|
|
||||||
|
body *binding.Body
|
||||||
}
|
}
|
||||||
|
|
||||||
/************************************/
|
/************************************/
|
||||||
@ -820,3 +823,29 @@ func (c *Context) Value(key interface{}) interface{} {
|
|||||||
}
|
}
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user