Getting Started
Core Concepts
Built-In Middleware
Advanced Features
Troubleshooting
In Hono, request handling revolves around the HonoRequest object, which is accessible via the context object (c.req) inside your route handlers. It provides a robust, developer-friendly interface to extract data from incoming HTTP requests, including path parameters, query strings, headers, cookies, and various body formats.
You can retrieve data from the request using simple method calls on c.req.
Note
When calling methods like .json(), .text(), or .formData(), always use await as these operations are asynchronous.
The hono/helper/cookie module provides utilities to read and set cookies easily.
getCookie(c, 'name') to retrieve a specific cookie value, or getCookie(c) to get all cookies.setCookie(c, 'name', 'value') to add a Set-Cookie header to the response.deleteCookie(c, 'name') to clear a cookie by setting its maxAge to 0.Tip
Use getSignedCookie and setSignedCookie if you need to cryptographically sign your cookies to prevent tampering.
You can validate incoming data using the validator middleware. This function intercepts the request, parses the specified target (like json, query, or form), runs your validation logic, and adds the result to the request object.
import { validator } from 'hono/validator'
app.post('/user', validator('json', (value, c) => {
if (!value.name) {
return c.json({ error: 'Name is required' }, 400)
}
return value
}), (c) => {
const data = c.req.valid('json')
return c.json({ success: true, user: data.name })
})Warning
If you consume a request body (e.g., calling c.req.json()) multiple times, Hono caches the result. Do not attempt to read the raw c.req.raw.body directly if you plan to use Hono's body parsing methods, as this may interfere with request cloning and middleware functionality.
If you need to process a request body multiple times or pass a request to an external service, you can use cloneRawRequest(req).
import { cloneRawRequest } from 'hono/request'
app.post('/forward', async (c) => {
const cloned = await cloneRawRequest(c.req)
return fetch('https://external-service.com', cloned)
})Important
cloneRawRequest only works if the body has been consumed via Hono's request methods (.json(), .text(), etc.). If the body was consumed elsewhere without caching, cloning will fail.