onSend hook
Runs after serialization, with access to the final payload before it is written.
Syntax
app.addHook('onSend', async (request, reply, payload) => any) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
request | FastifyRequest | No | The request object. |
reply | FastifyReply | No | The reply object. |
payload | string | Buffer | stream | null | No | The serialized response body. |
Returns
any — The (possibly modified) payload to send.
Examples
import Fastify from 'fastify';
const app = Fastify();
app.addHook('onSend', async (_req, reply, payload) => {
reply.header('x-content-type-options', 'nosniff');
return payload;
});
Notes
Receives the already-serialized payload (string/Buffer/stream). Return a
replacement to modify the body, or undefined to leave it unchanged. Last
chance to set headers.