Data Binding
Rue provides powerful data binding to parse request data into Go structs.
Binding Methods
type CreateUser struct {
Name string `json:"name" form:"name"`
Email string `json:"email" form:"email"`
Age int `json:"age" form:"age"`
}
r.POST("/users", func(c *rue.Context) {
var user CreateUser
// Bind JSON body
if err := c.BindJSON(&user); err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}
// Or bind form data
// c.BindForm(&user)
// Or bind query parameters
// c.BindQuery(&user)
// Or bind headers
// c.BindHeader(&user)
c.JSON(http.StatusCreated, user)
})
Supported Tags
| Tag | Source |
|---|---|
json:"name" | JSON body |
form:"name" | Form data |
query:"name" | Query string |
header:"X-Name" | HTTP headers |