Context API
2 minute read
Request Binding
Bind
func (c *Context) Bind(out any) error
Automatically binds from all relevant sources based on struct tags (path, query, header, cookie, json, form).
BindJSONStrict
func (c *Context) BindJSONStrict(out any) error
Binds JSON with unknown field rejection.
BindAndValidate
func (c *Context) BindAndValidate(out any, opts ...validation.Option) error
Binds and validates in one call.
BindAndValidateStrict
func (c *Context) BindAndValidateStrict(out any, opts ...validation.Option) error
Binds JSON strictly (rejects unknown fields) and validates.
MustBindAndValidate
func (c *Context) MustBindAndValidate(out any, opts ...validation.Option) bool
Binds and validates, automatically sending error responses on failure. Returns true if successful.
Error Handling
Error
func (c *Context) Error(err error)
Sends a formatted error response using the configured formatter.
ErrorStatus
func (c *Context) ErrorStatus(err error, status int)
Sends an error response with explicit status code.
NotFound
func (c *Context) NotFound(message string)
Sends a 404 Not Found error.
BadRequest
func (c *Context) BadRequest(message string)
Sends a 400 Bad Request error.
Unauthorized
func (c *Context) Unauthorized(message string)
Sends a 401 Unauthorized error.
Forbidden
func (c *Context) Forbidden(message string)
Sends a 403 Forbidden error.
InternalError
func (c *Context) InternalError(err error)
Sends a 500 Internal Server Error.
Logging
Logger
func (c *Context) Logger() *slog.Logger
Returns the request-scoped logger with automatic context (HTTP metadata, trace IDs, request ID). Never returns nil.
Presence
Presence
func (c *Context) Presence() validation.PresenceMap
Returns the presence map for the current request (tracks which fields were present in JSON).
ResetBinding
func (c *Context) ResetBinding()
Resets binding metadata (useful for testing).
Router Context
The app Context embeds router.Context, providing access to all router features:
c.Request- HTTP requestc.Response- HTTP response writerc.Param(name)- Path parameterc.Query(name)- Query parameterc.JSON(status, data)- Send JSON responsec.String(status, text)- Send text responsec.HTML(status, html)- Send HTML response- And more…
See Router Context API for complete router context reference.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.