Compare commits

...

2 Commits

Author SHA1 Message Date
Mohammad AlSammak
1507382feb
Merge 67d4ff8f6e6da871511bd2815edadcaa5c8f7b7f into 8763f33c65f7df8be5b9fe7504ab7fcf20abb41d 2025-03-21 16:22:30 +08:00
Mohammad AlSammak
67d4ff8f6e a temporary fix for issue 4120 2024-12-26 13:25:33 +03:00

View File

@ -5,7 +5,9 @@
package gin
import (
"bytes"
"errors"
"fmt"
"io"
"io/fs"
"log"
@ -722,6 +724,21 @@ func (c *Context) Bind(obj any) error {
// BindJSON is a shortcut for c.MustBindWith(obj, binding.JSON).
func (c *Context) BindJSON(obj any) error {
body, err := io.ReadAll(c.Request.Body)
if err != nil {
return fmt.Errorf("failed to read body: %v", err)
}
// If the body is empty, set it to {}
if len(body) == 0 {
body = []byte("{}")
c.Request.Body = io.NopCloser(bytes.NewBuffer(body)) // Set to {} to trigger the error
return c.MustBindWith(obj, binding.JSON)
}
// Restore the body since it was read
c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
return c.MustBindWith(obj, binding.JSON)
}