The Accepts Helper provides content negotiation functionality based on HTTP Accept headers.
Import
import { accepts } from 'hono/accepts'
Functions
accepts()
Determine the best match for the given Accept header.
function accepts(c: Context, type: string | string[]): string | null
type
string | string[]
required
The content type(s) to match against
The best matching content type or null if no match
Example
import { accepts } from 'hono/accepts'
app.get('/data', (c) => {
const accept = accepts(c, ['application/json', 'text/html'])
if (accept === 'application/json') {
return c.json({ data: 'value' })
} else if (accept === 'text/html') {
return c.html('<html>...</html>')
}
return c.text('Not Acceptable', 406)
})
Advanced Usage
import { accepts } from 'hono/accepts'
// Language negotiation
app.get('/', (c) => {
const lang = accepts(c, ['en', 'ja', 'fr'])
return c.text(`Language: ${lang}`)
})
// Compression negotiation
app.get('/file', (c) => {
const encoding = accepts(c, ['gzip', 'deflate', 'br'])
// Use the preferred encoding
})