Kind: Function
Source: src/helper/accepts/accepts.ts
Part of: Helper
Match the accept header with the given options.
accepts checks the request Accept header against the media types passed to it. It returns the matching option so route handlers can select an appropriate response representation.
Signature
tsfunction accepts(c: Context, options: acceptsOptions): string
Parameters
| Name | Type |
|---|---|
c | Context |
options | acceptsOptions |
Returns: string
Diagram
mermaidgraph LR Request[Incoming request] --> Context[Context] Context --> Accepts[accepts] Options[Supported media types] --> Accepts Accepts --> Match[Matching media type] Match --> Response[Route response]
Usage
tsimport { Hono } from 'hono'
import { accepts } from 'hono/accepts'
const app = new Hono()
app.get('/profile', (c) => {
const type = accepts(c, 'application/json', 'text/html')
if (type === 'text/html') {
return c.html('<h1>Profile</h1>')
}
if (type === 'application/json') {
return c.json({ name: 'Ada' })
}
return c.text('Not Acceptable', 406)
})
AI Coding Instructions
- Pass the Hono context as the first argument, followed by the media types the handler can return.
- Check the returned value before sending a representation, since no supplied type may match the request header.
- Keep media type strings aligned with the response methods used by the route.
- Return an appropriate unsupported-media response when no acceptable type is selected.
Was this page helpful?