preParsing hook
Runs before the request body is parsed, allowing the raw stream to be transformed.
Syntax
app.addHook('preParsing', async (request, reply, payload) => stream) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
request | FastifyRequest | No | The incoming request. |
reply | FastifyReply | No | The reply object. |
payload | Readable | No | The raw request body stream. |
Returns
Readable | undefined — Return a replacement stream or nothing.
Examples
import Fastify from 'fastify';
import { Transform } from 'node:stream';
const app = Fastify();
app.addHook('preParsing', async (_req, _reply, payload) => {
// pass through; could decompress or count bytes here
return payload.pipe(new Transform({
transform(chunk, _enc, cb) { cb(null, chunk); },
}));
});
Notes
Runs after onRequest but before content-type parsing. Return a new
Readable to replace the incoming stream (e.g. decryption, decompression).
Returning undefined leaves the stream unchanged.