# observe\_middleware/3

Source: https://zotonic.com/docs/2319/observe_middleware-3

URI: https://zotonic.com/id/2319

Language: en



Delegates the request processing.

Type:

[foldl](/id/doc_developerguide_notifications#notification-foldl)

Return:

updated `z:context()`

`#middleware{}` properties:

- on: `guard|rejected|request|welformed|handled`

The `guard` phase runs before controller callbacks, with proxy and cookie metadata initialized. It can return a context with `request_guard_response = {Status, Body}` (an HTTP 400..599 status and binary body). Response headers set on that context are preserved. Before sending this response, core emits `rejected` synchronously. It does not run `request`, `welformed`, `handled`, or controller rendering. The returned context from `rejected` is ignored and observer exceptions cannot change the rejection response. Observers must return the context for other observers and initialize authentication themselves if needed; early requests are not authenticated. Keep rejection observers bounded and best-effort: they execute on the request process.

Guards can set `request_guard_result` to a server-owned map containing `source` (the producing module atom), `guard_action`, `guard_reason`, and `crawl_behavior` (binary classifications). Actions are `allow`, `would_throttle`, or `throttle`; crawl behavior is `normal`, `excessive`, or `unknown`. Reasons are producer-defined bounded values, not client input. Consumers must select supported sources and fields, never forward the entire map or include client identifiers. Guards may keep additional internal fields such as `retry_after`. This contract belongs to core, not a consumer. For normally handled requests, the result remains available in the `handled` phase. Optional telemetry consumers decide whether any enrichment or recording is needed.

License: 

Attribution: 

Related resources: https://zotonic.com/api/agent/related/2319?language=en


## API access

- [API help](<https://zotonic.com/api-help>)
- [OpenAPI specification](<https://zotonic.com/openapi.json>)
- [This resource as JSON](<https://zotonic.com/api/agent/resource/2319?language=en>)
