ctx.request.get()
Returns the value of a specific request header field (case-insensitive).
Syntax
ctx.request.get(field) // or ctx.get(field) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
field | string | Yes | The header field name. Case-insensitive. `"Referrer"` and `"Referer"` are treated as aliases. |
Returns
string — The header value, or an empty string `""` if not present.
Examples
import Koa from 'koa';
const app = new Koa();
app.use(async (ctx) => {
const auth = ctx.get('Authorization');
const ua = ctx.get('User-Agent');
if (!auth) ctx.throw(401, 'Authorization header required');
ctx.body = { ua };
});
app.listen(3000);
Output
curl -H 'Authorization: Bearer token123' localhost:3000
{"ua":"curl/8.7.1"}
Notes
Returns `""` (empty string) rather than `undefined` when the header is
absent, making it safe to call `.startsWith()` etc. without null checks.
The `Referrer`/`Referer` normalisation follows the HTTP spec.