MCPcopy
hub / github.com/labstack/echo / BindBody

Function BindBody

bind.go:68–111  ·  view source on GitHub ↗

BindBody binds request body contents to bindable object NB: then binding forms take note that this implementation uses standard library form parsing which parses form data from BOTH URL and BODY if content type is not MIMEMultipartForm See non-MIMEMultipartForm: https://golang.org/pkg/net/http/#Requ

(c *Context, target any)

Source from the content-addressed store, hash-verified

66// See non-MIMEMultipartForm: https://golang.org/pkg/net/http/#Request.ParseForm
67// See MIMEMultipartForm: https://golang.org/pkg/net/http/#Request.ParseMultipartForm
68func BindBody(c *Context, target any) (err error) {
69 req := c.Request()
70 if req.ContentLength == 0 {
71 return
72 }
73
74 // mediatype is found like `mime.ParseMediaType()` does it
75 base, _, _ := strings.Cut(req.Header.Get(HeaderContentType), ";")
76 mediatype := strings.TrimSpace(base)
77
78 switch mediatype {
79 case MIMEApplicationJSON:
80 if err = c.Echo().JSONSerializer.Deserialize(c, target); err != nil {
81 var hErr *HTTPError
82 if errors.As(err, &hErr) {
83 return err
84 }
85 return ErrBadRequest.Wrap(err)
86 }
87 case MIMEApplicationXML, MIMETextXML:
88 if err = xml.NewDecoder(req.Body).Decode(target); err != nil {
89 return ErrBadRequest.Wrap(err)
90 }
91 case MIMEApplicationForm:
92 params, err := c.FormValues()
93 if err != nil {
94 return ErrBadRequest.Wrap(err)
95 }
96 if err = bindData(target, params, "form", nil); err != nil {
97 return ErrBadRequest.Wrap(err)
98 }
99 case MIMEMultipartForm:
100 params, err := c.MultipartForm()
101 if err != nil {
102 return ErrBadRequest.Wrap(err)
103 }
104 if err = bindData(target, params.Value, "form", params.File); err != nil {
105 return ErrBadRequest.Wrap(err)
106 }
107 default:
108 return &HTTPError{Code: http.StatusUnsupportedMediaType}
109 }
110 return nil
111}
112
113// BindHeaders binds HTTP headers to a bindable object
114func BindHeaders(c *Context, target any) error {

Callers 2

BindMethod · 0.85

Calls 8

bindDataFunction · 0.85
RequestMethod · 0.80
EchoMethod · 0.80
FormValuesMethod · 0.80
MultipartFormMethod · 0.80
DeserializeMethod · 0.65
GetMethod · 0.45
WrapMethod · 0.45

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…