ctx.request.method

The HTTP request method in upper-case (e.g. `"GET"`, `"POST"`, `"PUT"`).

Since Koa 2 Spec ↗

Syntax

ctx.request.method  // or ctx.method

Returns

string — Upper-case HTTP method string.

Examples

import Koa from 'koa';

const app = new Koa();

app.use(async (ctx) => {
  if (ctx.method === 'GET') {
    ctx.body = 'Fetching resource';
  } else if (ctx.method === 'POST') {
    ctx.body = 'Creating resource';
  } else {
    ctx.throw(405, 'Method Not Allowed');
  }
});

app.listen(3000);
Output
GET /  →  200 "Fetching resource"
POST / →  200 "Creating resource"
DELETE / → 405 Method Not Allowed

Notes

`ctx.method` is a convenient alias for `ctx.request.method`. When using `koa-router`, method routing is handled automatically via `router.get()`, `router.post()`, etc.

See also