Chapter 7

Response Patterns

Return text, JSON, HTML, redirects, headers, and status codes.

Response Patterns

Hono handlers return responses. You can return a Web Standard Response, but most routes are clearer with Hono's response helpers.

app.get('/health', (c) => {
  return c.text('OK')
})

Common helpers

Use the helper that matches the shape of the response.

app.get('/api/chapters', (c) => {
  return c.json({ chapters: ['Introduction', 'Getting Started'] })
})

app.get('/old-toc', (c) => {
  return c.redirect('/toc')
})

app.get('/hello', (c) => {
  return c.html('<h1>Hello Hono</h1>')
})

This book uses c.redirect() for /toc and c.render() for full HTML pages.

Status codes

Pass a status code as the second argument when the helper supports it.

app.post('/api/chapters', (c) => {
  return c.json({ ok: true }, 201)
})

You can also set the status before returning.

app.get('/private', (c) => {
  c.status(403)
  return c.text('Forbidden')
})

Response headers

Use c.header() before returning the response.

app.get('/download', (c) => {
  c.header('Content-Disposition', 'attachment; filename="chapters.json"')
  return c.json({ ok: true })
})

Headers are useful for caching, content negotiation, debugging, and security. Middleware is often the best place for headers that apply to many routes.