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)
| 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. |
| 928 | func (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). |
| 946 | func (c *Context) ShouldBindBodyWithJSON(obj any) error { |