Accept, Accept-Language, Accept-Encoding) against your supported options. It returns the best match based on client preferences and quality values.
Import
Basic Usage
Match theAccept header to determine response format:
Parameters
Context
required
The Hono context object
acceptsOptions
required
Configuration object for content negotiation
Options Object
AcceptHeader
required
The header to match against. Common values:
'Accept'- Content type'Accept-Language'- Language preference'Accept-Encoding'- Compression method
string[]
required
Array of values your application supports (e.g.,
['en', 'ja', 'zh'])string
required
Default value to return if no match is found or header is missing
function
Custom matching function to override default behavior. Receives parsed accepts array and config.
string - The best matching supported value or the default
Content Type Negotiation
Match theAccept header for API responses:
Language Negotiation
Automatic language selection based on user preferences:Compression Negotiation
Select compression method based on client support:Custom Matching Logic
Provide a custom match function for specialized behavior:Quality Values
The helper automatically respects quality values (q-values) in Accept headers:Type Definitions
Default Matching Behavior
The default matcher:- Parses the Accept header into an array of
Acceptobjects - Sorts by quality value (q) in descending order
- Finds the first value that matches your
supportsarray - Returns the matched value or the
defaultif no match
Best Practices
Always Provide Default
Always specify a sensible default value for cases where the header is missing or no match is found
Order Matters
List supported values in order of preference when multiple have the same quality value
Respect Standards
Use standard MIME types for Accept header (
application/json, not json)Test Edge Cases
Test with missing headers and wildcard values like
*/*When the Accept header is missing or empty, the helper returns the default value immediately without attempting to match.