Skip to main content
doc0blade47/honodocumentation home
Sign in

Context API Methods

Last updated Oct 12, 2026

Overview

The Context object (c) is passed to every route handler and middleware in your application. It acts as the central hub for handling incoming HTTP requests and generating outgoing responses. Using Context methods, you can inspect URL paths, read query parameters, fetch request headers, parse incoming request bodies, manage cookies, and send text, JSON, HTML, or redirected responses back to the client.

Inspecting Requests

Path Parameters and URL Paths

You can retrieve captured path parameters from dynamic routes or inspect the current request path using c.req.

app.get('/posts/:id', (c) => {
  // Get a single path parameter
  const id = c.req.param('id')
 
  // Or get all path parameters as an object
  const params = c.req.param()
 
  // Get the raw pathname of the request
  const pathname = c.req.path
 
  return c.text(`Post ID: ${id}, Path: ${pathname}`)
})

Query and Multi-Value Query Parameters

Query parameters attached to the URL string can be retrieved individually or all at once. For parameters sent multiple times (e.g., ?tags=A&tags=B), use .queries().

app.get('/search', (c) => {
  // Get a single query parameter value
  const searchQuery = c.req.query('q')
 
  // Get all query parameters as an object
  const allQueryParams = c.req.query()
 
  // Get all values for a repeating query parameter as an array (e.g., string[])
  const tags = c.req.queries('tags')
 
  return c.json({ searchQuery, allQueryParams, tags })
})

Request Headers

You can inspect individual request headers using case-insensitive header names, or retrieve all request headers as a record object.

app.get('/profile', (c) => {
  // Get a specific header value
  const userAgent = c.req.header('User-Agent')
 
  // Get all request headers
  const allHeaders = c.req.header()
 
  return c.json({ userAgent, allHeaders })
})

Reading Request Bodies

Parsing Form Data and URL-Encoded Bodies

Use c.req.parseBody() to parse incoming multipart/form-data or application/x-www-form-urlencoded request bodies from POST, PUT, or PATCH requests.

app.post('/entry', async (c) => {
  // Parse form fields and uploaded files
  const body = await c.req.parseBody()
  
  return c.json({ received: body })
})

Tip

c.req.parseBody() accepts options such as { all: true } to collect repeating field names into arrays, and { dot: true } to parse dot-notation keys into nested JavaScript objects.

Managing Cookies

Reading, Setting, and Deleting Cookies

Cookies can be read, set, and deleted using helper functions imported from 'hono/cookie'.

import { Hono } from 'hono'
import { getCookie, setCookie, deleteCookie } from 'hono/cookie'
 
const app = new Hono()
 
app.get('/set-cookie', (c) => {
  // Set a standard cookie
  setCookie(c, 'username', 'Alice', {
    path: '/',
    secure: true,
    httpOnly: true,
    maxAge: 3600,
  })
 
  return c.text('Cookie has been set!')
})
 
app.get('/get-cookie', (c) => {
  // Read a cookie by name
  const username = getCookie(c, 'username')
 
  // Or delete a cookie
  // deleteCookie(c, 'username')
 
  return c.text(`Hello, ${username || 'Guest'}!`)
})

Sending Responses

Available Response Methods

MethodDescriptionContent-Type Header Default
c.text(text, status?, headers?)Sends a plain text response.text/plain; charset=UTF-8
c.json(object, status?, headers?)Serializes an object to JSON and sends it.application/json
c.html(html, status?, headers?)Renders HTML strings or promises.text/html; charset=UTF-8
c.body(data, status?, headers?)Sends raw body content (BodyInit).None (unless specified)
c.redirect(location, status?)Redirects to another location (default status 302).Location header set
c.notFound()Returns the application's configured Not Found response.N/A

Example Usage of Response Methods

app.get('/welcome', (c) => {
  // Set custom headers and status code beforehand if needed
  c.header('X-Custom-Header', 'Hono-Value')
  c.status(200)
 
  // Send a JSON response
  return c.json({ message: 'Welcome to the API!' })
})
 
app.get('/old-route', (c) => {
  // Redirect to a new location with a 301 Permanent Redirect
  return c.redirect('/welcome', 301)
})
Was this page helpful?