16mb BindBody unmarshal the request body into the provided dst. dst must be either a struct pointer or map[string]any. The rules how the body will be scanned depends on the request Content-Type. Currently the following Content-Types are supported: - application/json - text/xml, application/xml -
(dst any)
| 352 | // }{} |
| 353 | // err := e.BindBody(&data) |
| 354 | func (e *Event) BindBody(dst any) error { |
| 355 | if e.Request.ContentLength == 0 { |
| 356 | return nil |
| 357 | } |
| 358 | |
| 359 | contentType := e.Request.Header.Get(headerContentType) |
| 360 | |
| 361 | if strings.HasPrefix(contentType, "application/json") { |
| 362 | dec := json.NewDecoder(e.Request.Body) |
| 363 | err := dec.Decode(dst) |
| 364 | if err == nil { |
| 365 | // manually call Reread because single call of json.Decoder.Decode() |
| 366 | // doesn't ensure that the entire body is a valid json string |
| 367 | // and it is not guaranteed that it will reach EOF to trigger the reread reset |
| 368 | // (ex. in case of trailing spaces or invalid trailing parts like: `{"test":1},something`) |
| 369 | if body, ok := e.Request.Body.(Rereader); ok { |
| 370 | body.Reread() |
| 371 | } |
| 372 | } |
| 373 | return err |
| 374 | } |
| 375 | |
| 376 | if strings.HasPrefix(contentType, "multipart/form-data") { |
| 377 | if err := e.Request.ParseMultipartForm(DefaultMaxMemory); err != nil { |
| 378 | return err |
| 379 | } |
| 380 | |
| 381 | return UnmarshalRequestData(e.Request.Form, dst, "", "") |
| 382 | } |
| 383 | |
| 384 | if strings.HasPrefix(contentType, "application/x-www-form-urlencoded") { |
| 385 | if err := e.Request.ParseForm(); err != nil { |
| 386 | return err |
| 387 | } |
| 388 | |
| 389 | return UnmarshalRequestData(e.Request.Form, dst, "", "") |
| 390 | } |
| 391 | |
| 392 | if strings.HasPrefix(contentType, "text/xml") || |
| 393 | strings.HasPrefix(contentType, "application/xml") { |
| 394 | return xml.NewDecoder(e.Request.Body).Decode(dst) |
| 395 | } |
| 396 | |
| 397 | return ErrUnsupportedContentType |
| 398 | } |