Files
2026-10-30 18:11:30 +03:00

36 lines
723 B
Go

package common
import (
"bytes"
"encoding/json"
"io"
"strings"
"github.com/gofiber/fiber/v3"
)
func BindJSON(c fiber.Ctx, dst any) error {
if contentType := c.Get(fiber.HeaderContentType); !strings.HasPrefix(contentType, "application/json") {
return UnsupportedMedia("invalid JSON body")
}
body := c.Body()
if len(body) == 0 {
return BadRequest("request body is empty")
}
decoder := json.NewDecoder(bytes.NewReader(body))
decoder.DisallowUnknownFields()
if err := decoder.Decode(dst); err != nil {
return BadRequest("invalid JSON body")
}
var extra json.RawMessage
if err := decoder.Decode(&extra); err != io.EOF {
return BadRequest("request body has extra trailing bytes")
}
return nil
}