Chapter 6

Request Context

Read request data from Hono's context object.

Request Context

Every Hono handler receives a context object, usually named c. The context belongs to one request and gives you access to the incoming request, the runtime environment, and response helpers.

app.get('/chapters/:slug', (c) => {
  const slug = c.req.param('slug')
  return c.text(`Reading ${slug}`)
})

This book uses the same pattern in its chapter route. The URL decides which Markdown chapter to load.

Route parameters

Use c.req.param() for values captured from the route path.

app.get('/users/:id/posts/:postId', (c) => {
  const { id, postId } = c.req.param()
  return c.json({ id, postId })
})

Query strings

Use c.req.query() for optional values after ?.

app.get('/search', (c) => {
  const q = c.req.query('q') ?? ''
  return c.text(`Searching for ${q}`)
})

For repeated query values, use c.req.queries().

const tags = c.req.queries('tag') ?? []

Headers and raw requests

Request headers are available through c.req.header().

const userAgent = c.req.header('User-Agent')

When you need the Web Standard request object directly, use c.req.raw.

app.get('/debug', (c) => {
  return c.json({
    method: c.req.raw.method,
    url: c.req.raw.url,
  })
})

Most routes should use Hono's request helpers first. Reach for the raw request when you need a platform-specific field or an API that Hono does not wrap.