ctx.request.is()

Checks whether the incoming `Content-Type` header matches one of the given MIME types.

Since Koa 2 Spec ↗

Syntax

ctx.request.is(...types)  // or ctx.is(...types)

Parameters

NameTypeRequiredDescription
types ...string Yes One or more MIME types or short extensions (e.g. `"json"`, `"application/json"`, `"multipart/*"`). Supports wildcards.

Returns

string | false | null — The matching type string, `false` if no match, or `null` when the request has no body.

Examples

import Koa from 'koa';

const app = new Koa();

app.use(async (ctx) => {
  if (ctx.is('json')) {
    // parse JSON body (e.g. via koa-bodyparser)
    ctx.body = { received: 'json' };
  } else if (ctx.is('multipart/*')) {
    ctx.body = { received: 'multipart' };
  } else {
    ctx.throw(415, 'Unsupported Media Type');
  }
});

app.listen(3000);
Output
POST (Content-Type: application/json) → {"received":"json"}
POST (Content-Type: multipart/form-data) → {"received":"multipart"}

Notes

Uses the [type-is](https://github.com/jshttp/type-is) library. Does not parse the body — it only inspects the `Content-Type` header. Combine with `koa-bodyparser` for actual body parsing. Use `ctx.accepts()` for response content negotiation.

See also