MustBindWith binds the passed struct pointer using the specified binding engine. It will abort the request with HTTP 400 if any error occurs. See the binding package.
(obj any, b binding.Binding)
| 808 | // It will abort the request with HTTP 400 if any error occurs. |
| 809 | // See the binding package. |
| 810 | func (c *Context) MustBindWith(obj any, b binding.Binding) error { |
| 811 | err := c.ShouldBindWith(obj, b) |
| 812 | if err != nil { |
| 813 | var maxBytesErr *http.MaxBytesError |
| 814 | |
| 815 | // Note: When using sonic or go-json as JSON encoder, they do not propagate the http.MaxBytesError error |
| 816 | // https://github.com/goccy/go-json/issues/485 |
| 817 | // https://github.com/bytedance/sonic/issues/800 |
| 818 | switch { |
| 819 | case errors.As(err, &maxBytesErr): |
| 820 | c.AbortWithError(http.StatusRequestEntityTooLarge, err).SetType(ErrorTypeBind) //nolint: errcheck |
| 821 | default: |
| 822 | c.AbortWithError(http.StatusBadRequest, err).SetType(ErrorTypeBind) //nolint: errcheck |
| 823 | } |
| 824 | return err |
| 825 | } |
| 826 | return nil |
| 827 | } |
| 828 | |
| 829 | // ShouldBind checks the Method and Content-Type to select a binding engine automatically, |
| 830 | // Depending on the "Content-Type" header different bindings are used, for example: |