Skip to main content
Every handler and middleware receives a *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.
Two fields are always available directly:

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

Pass 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 with app.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

Use ctx.Success and ctx.Fail to return a consistent envelope shape across your entire API without repeating the structure in every handler.
Success always returns HTTP 200 with code: 0:
Fail always returns HTTP 200 with a non-zero application error code and a message:
This pattern lets clients always check the HTTP status for transport errors and the 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:
Data is scoped to the single request — it is cleared when the request completes.