Getting Started
Core Concepts
Built-In Middleware
Advanced Features
Troubleshooting
Response handling in Hono is managed through the Context object, which provides a unified API for generating HTTP responses. Whether you are returning plain text, JSON, or HTML, the Context ensures your response is correctly formatted and allows you to easily attach headers or modify status codes.
Hono provides several helper methods on the Context object to send data back to the client. These methods automatically set the appropriate Content-Type header based on the data format.
app.get('/api/user', (c) => {
return c.json({ name: 'Hono' })
})
app.get('/greet', (c) => {
return c.text('Hello, world!')
})You can customize the response by chaining configuration methods before returning the final response, or by passing options directly to the helper methods.
.status() and .header(): Call these methods on the context to modify the state of the current request handler..text(), .json(), etc.) to finalize the response.app.get('/custom', (c) => {
c.status(201)
c.header('X-Custom-Header', 'my-value')
return c.text('Created!')
})Tip
You can also pass an optional status code or headers object directly into the response helper: c.json({ success: true }, 202).
To redirect a user, use the .redirect() method. It takes a URL string and an optional HTTP status code (which defaults to 302 Found).
app.get('/go-home', (c) => {
return c.redirect('/')
})
app.get('/moved', (c) => {
return c.redirect('/new-location', 301)
})For large responses or long-running tasks, use the stream helpers imported from the streaming module. This allows you to send data chunks to the client over time.
import { streamText } from 'hono/streaming'
app.get('/stream', (c) => {
return streamText(c, async (stream) => {
await stream.write('Hello ')
await stream.writeln('World!')
})
})Important
Once you return a response from a handler, the context is marked as finalized. Attempting to modify headers or status codes after returning may lead to unexpected behavior if not handled via the c.res property.