Context API to access every part of an incoming request. All methods live on the *Context value passed to your handler.
URL Parameters
Define dynamic segments in your route pattern with a: prefix. Read them at runtime with ctx.Param().
(T, error) so you can handle malformed values explicitly:
| Method | Return type |
|---|---|
ctx.ParamInt(key) | (int, error) |
ctx.ParamInt64(key) | (int64, error) |
ctx.ParamUInt(key) | (uint, error) |
ctx.ParamUInt64(key) | (uint64, error) |
ctx.ParamFloat32(key) | (float32, error) |
ctx.ParamFloat64(key) | (float64, error) |
ctx.Params() to get all URL parameters as map[string]string at once.
Query Parameters
Read query string values withctx.Query() for strings or the typed helpers for numeric and boolean values.
| Method | Return type |
|---|---|
ctx.QueryInt(key) | (int, error) |
ctx.QueryInt8(key) | (int8, error) |
ctx.QueryInt32(key) | (int32, error) |
ctx.QueryInt64(key) | (int64, error) |
ctx.QueryUInt(key) | (uint, error) |
ctx.QueryUInt8(key) | (uint8, error) |
ctx.QueryUInt32(key) | (uint32, error) |
ctx.QueryUInt64(key) | (uint64, error) |
ctx.QueryFloat32(key) | (float32, error) |
ctx.QueryFloat64(key) | (float64, error) |
ctx.QueryBool(key) | (bool, error) |
ctx.Queries() to get all query parameters as map[string][]string.
Request Body
JSON body
Usectx.JSONBody(&v) to decode the request body into a struct. Pass true as the second argument to also validate the struct using go-playground/validator struct tags.
false) to skip validation and decode only:
Raw and string body
Headers
Read a single request header by name withctx.Header(). Header names are case-insensitive.
ctx.Headers() to get all request headers as map[string]string.
Cookies
Read a single cookie by name withctx.Cookie(). It returns *fasthttp.Cookie (or nil if the cookie is not present).
ctx.Cookies() to iterate over all cookies in the request:
Complete Example: POST Handler with JSON Validation
The following handler parses a JSON body, validates it, and returns a structured response.ctx.JSONBody returns the first validation error as a Go error, and ctx.Fail(-1, err.Error()) sends it back to the client.