ctx.response.get()

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

Since Koa 2 Spec ↗

Syntax

ctx.response.get(field)

Parameters

NameTypeRequiredDescription
field string Yes The header field name (case-insensitive).

Returns

string — The header value, or `""` if the header has not been set.

Examples

import Koa from 'koa';

const app = new Koa();

app.use(async (ctx, next) => {
  await next();
  // Check and extend cache header set by inner middleware
  const cc = ctx.response.get('Cache-Control');
  if (!cc) {
    ctx.set('Cache-Control', 'no-store');
  }
});

app.use(async (ctx) => {
  ctx.set('Cache-Control', 'max-age=60');
  ctx.body = 'ok';
});

app.listen(3000);
Output
Cache-Control: max-age=60

Notes

Useful in outer middleware layers that need to inspect headers set by inner middleware after `await next()`. For request headers, use `ctx.get(field)` (or `ctx.request.get(field)`).

See also