reply.getHeader()
Returns the value of a response header that has been set.
Syntax
reply.getHeader(name: string) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
name | string | No | The header name to read. |
Returns
string | number | string[] | undefined — The header value if set.
Examples
import Fastify from 'fastify';
const app = Fastify();
app.addHook('onSend', async (_req, reply, payload) => {
if (!reply.getHeader('cache-control')) {
reply.header('cache-control', 'no-store');
}
return payload;
});
Notes
Useful in onSend hooks to inspect and conditionally add headers. Returns
undefined if the header has not been set. Use getHeaders() for all set
headers.