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

Method ShouldBindBodyWith

context.go:928–943  ·  context.go::Context.ShouldBindBodyWith

ShouldBindBodyWith is similar with ShouldBindWith, but it stores the request body into the context, and reuse when it is called again. NOTE: This method reads the body before binding. So you should use ShouldBindWith for better performance if you need to call only once.

(obj any, bb binding.BindingBody)

Source from the content-addressed store, hash-verified

926// NOTE: This method reads the body before binding. So you should use
927// ShouldBindWith for better performance if you need to call only once.
928func (c *Context) ShouldBindBodyWith(obj any, bb binding.BindingBody) (err error) {
929 var body []byte
930 if cb, ok := c.Get(BodyBytesKey); ok {
931 if cbb, ok := cb.([]byte); ok {
932 body = cbb
933 }
934 }
935 if body == nil {
936 body, err = io.ReadAll(c.Request.Body)
937 if err != nil {
938 return err
939 }
940 c.Set(BodyBytesKey, body)
941 }
942 return bb.BindBody(body, obj)
943}
944
945// ShouldBindBodyWithJSON is a shortcut for c.ShouldBindBodyWith(obj, binding.JSON).
946func (c *Context) ShouldBindBodyWithJSON(obj any) error {

Callers 6

ShouldBindBodyWithXMLMethod · 0.95

Calls 3

GetMethod · 0.95
SetMethod · 0.95
BindBodyMethod · 0.65

Tested by 1