MCPcopy
hub / github.com/gin-gonic/gin / MustBindWith

Method MustBindWith

context.go:810–827  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

808// It will abort the request with HTTP 400 if any error occurs.
809// See the binding package.
810func (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:

Callers 9

BindWithMethod · 0.95
BindMethod · 0.95
BindJSONMethod · 0.95
BindXMLMethod · 0.95
BindQueryMethod · 0.95
BindYAMLMethod · 0.95
BindTOMLMethod · 0.95
BindPlainMethod · 0.95
BindHeaderMethod · 0.95

Calls 3

ShouldBindWithMethod · 0.95
AbortWithErrorMethod · 0.95
SetTypeMethod · 0.80

Tested by

no test coverage detected