reply.getHeader()

Returns the value of a response header that has been set.

Since Fastify 5 Spec ↗

Syntax

reply.getHeader(name: string)

Parameters

NameTypeRequiredDescription
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.