*lightning.Context. It wraps the underlying HTTP request and response and provides a clean API for reading input, writing output, and sharing data across the middleware chain.
Reading request data
URL parameters
URL parameters are the named segments defined in the route pattern (e.g.:id).
Query parameters
Query methods that parse a numeric type return
0 (not an error) when the key is absent from the URL.Request body
true as the second argument to JSONBody to also run struct validation (powered by go-playground/validator):
Headers
Cookies
Client metadata
Writing responses
JSON
Plain text
HTML
Load templates once at startup withapp.LoadHTMLGlob, then render by template name:
XML
File download
Redirect
Error response
JSONError returns an HTTP error status code alongside a JSON body:
Success and Fail helpers
Success always returns HTTP 200 with code: 0:
Fail always returns HTTP 200 with a non-zero application error code and a message:
code field for application errors.
Per-request data store
SetData, GetData, and DelData let you attach arbitrary values to the current request context. This is useful for passing data from middleware to downstream handlers without mutating global state.
GetData returns any, so you need a type assertion. DelData removes a key: