Documentation ¶
Index ¶
- Constants
- Variables
- func Bind(c *gin.Context, obj interface{}) error
- func BindBody(c *gin.Context, obj interface{}) error
- func BindQuery(c *gin.Context, obj interface{}) error
- func MustBindWith(c *gin.Context, obj interface{}, b Binding) error
- func NewReq[REQ any](c *gin.Context) (*REQ, error)
- func RegisterBodyBinding(name string, unmarshaller func(data []byte, obj any) error)
- func RegisterBodyBindingByDecoder(name string, newDecoder func(io.Reader) encoding.Decoder)
- func SetTag(tag string)
- func ShouldBind(c *gin.Context, obj interface{}) error
- func ShouldBindBody(c *gin.Context, obj interface{}) error
- func ShouldBindBodyWith(c *gin.Context, obj interface{}, bb BindingBody) (err error)
- func ShouldBindQuery(c *gin.Context, obj interface{}) error
- func ShouldBindUri(r *gin.Context, obj interface{}) error
- func ShouldBindWith(c *gin.Context, obj interface{}, b Binding) error
- func Validate(obj interface{}) error
- type Binding
- type BindingBody
Constants ¶
const ( MIMEPOSTForm = "application/x-www-form-urlencoded" MIMEMultipartPOSTForm = "multipart/form-data" )
Variables ¶
var ( Query = queryBinding{} FormPost = formPostBinding{} FormMultipart = formMultipartBinding{} Uri = uriBinding{} Header = headerBinding{} CustomBody = bodyBinding{/* contains filtered or unexported fields */} )
These implement the Binding interface and can be used to bind the data present in the request to struct instances.
Functions ¶
func MustBindWith ¶
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.
func RegisterBodyBinding ¶ added in v0.1.9
func RegisterBodyBindingByDecoder ¶ added in v0.1.9
func ShouldBind ¶
ShouldBind checks the Content-Type to select a binding engine automatically, Depending the "Content-Type" header different bindings are used:
"application/json" --> JSON binding "application/xml" --> XML binding
otherwise --> returns an error It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input. It decodes the json payload into the struct specified as a pointer. Like c.GinBind() but this method does not set the response status code to 400 and abort if the json is not valid.
func ShouldBindBody ¶ added in v0.1.9
func ShouldBindBodyWith ¶
func ShouldBindBodyWith(c *gin.Context, obj interface{}, bb BindingBody) (err error)
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.
func ShouldBindQuery ¶
ShouldBindQuery is a shortcut for c.ShouldBindWith(obj, binding.Query).
func ShouldBindUri ¶
ShouldBindUri binds the passed struct pointer using the specified binding engine.
func ShouldBindWith ¶
ShouldBindWith binds the passed struct pointer using the specified binding engine. See the binding package.
Types ¶
type Binding ¶
Binding describes the interface which needs to be implemented for binding the data present in the request such as JSON request body, query parameters or the form POST.
type BindingBody ¶
BindingBody adds BindBody method to Binding. BindBody is similar with GinBind, but it reads the body from supplied bytes instead of req.Body.