Request Binding
Zen provides struct-tag-based binding for request bodies, path parameters, query parameters, and headers. Binding uses reflection with no intermediate caching - each call parses fresh.
JSON
Decodes the request body as JSON. Uses json.Decoder for streaming:
var req CreateUser
if err := c.BindJSON(&req); err != nil {
c.Error(400, err.Error())
return
}
XML
Decodes the request body as XML. Uses xml.Decoder for streaming:
var req XMLRequest
if err := c.BindXML(&req); err != nil {
c.Error(400, err.Error())
return
}
Form
Decodes application/x-www-form-urlencoded or multipart/form-data bodies.
Uses form struct tags:
type LoginForm struct {
Username string `form:"username"`
Password string `form:"password"`
}
var form LoginForm
if err := c.BindForm(&form); err != nil {
c.Error(400, err.Error())
return
}
Query Parameters
Binds URL query parameters to struct fields using query tags.
Falls back to json tag if query is absent:
type Search struct {
Query string `query:"q"`
Page int `query:"page"`
}
var q Search
if err := c.BindQueryParams(&q); err != nil {
c.Error(400, err.Error())
return
}
Supports repeated query params: ?id=1&id=2 binds as []string{"1", "2"} when the struct field type is []string.
Path Parameters
Binds URL path parameters to struct fields using param tags.
Falls back to json tag if param is absent:
type Params struct {
ID string `param:"id"`
PostID string `param:"postId"`
}
var p Params
if err := c.BindPathParams(&p); err != nil {
c.Error(400, err.Error())
return
}
Headers
Binds request headers to struct fields using header tags:
type Headers struct {
UserID string `header:"X-User-Id"`
APIToken string `header:"X-Api-Token"`
}
var h Headers
if err := c.BindHeader(&h); err != nil {
c.Error(400, err.Error())
return
}
Auto-Detect
c.Bind() detects content type and binds body, path parameters, and query parameters
in one call. For GET/DELETE/HEAD, it also binds query parameters:
c.Bind(&req) // detects content type, binds path/query/body
Supported Field Types
| Go Type | Input Format |
|---|---|
string | raw value |
int, int8-int64 | decimal string |
uint, uint8-uint64 | decimal string |
float32, float64 | decimal string |
bool | true/TRUE/True/1/yes/on |
[]string | repeated params |