Programming API
Request/Response Hooks
Hooks allow running code as part of the Request/Response pipeline. Hooks are typically added using a plugin as shown in this document, but they can also be added globally using Runtime Extensions.
Tip
All hooks can be either synchronous or asynchronous. To make your hook
asynchronous simply add the async
keyword on the function.
Hook: OnResponseSending#
The OnResponseSending
hook on ZuploContext
fires just before the response is
sent to the client. The Response
can be modified by returning a new Response
from this hook. This hook is useful for creating an inbound policy that also
needs to run some logic after the response is returned from the handler.
The example below shows a simple tracing policy that adds a trace header to the request and ensures the same header is returned with the response.
Hook: OnResponseSendingFinal#
The OnResponseSendingFinal
hook on ZuploContext
fires immediately before the
response is sent to the client. The Response
in this hook is immutable. This
hook is useful for custom tasks like caching, logging or analytics.
This simple example shows logging the time from the start of this policy until just before the response is sent.
Note that the call can block the response, so if you wish to run an activity asynchronously
and not block the response you should not await inside the hook and use context.waitUntil
to ensure your async activity is not terminated prematurely - this is shown in the example
below. This example shows details of the request and final response being sent to an imaginary
logging service.