Skip to content

accepts

Validates the client’s Accept, Accept-Language, Accept-Charset, and Accept-Encoding request headers against the server’s supported values. Returns 406 Not Acceptable when any negotiation fails (configurable).

Pipeline stage: Negotiation

import {accepts} from '@centralping/ergo';
Option Type Default Description
throwIfFail boolean true Return 406 when any negotiated value is undefined
types string[] Acceptable media types
languages string[] Acceptable languages
charsets string[] Acceptable character sets
encodings string[] Acceptable content encodings

On success, the middleware returns a negotiation result stored on the domain accumulator at acc.accepts:

{
type: 'application/json',
language: 'en',
charset: 'utf-8',
encoding: 'identity'
}

Each key corresponds to the best match for the respective header. When no constraint is provided for a key, the negotiated value is the client’s top preference.

Status Condition
406 Not Acceptable Any negotiated value is undefined and throwIfFail is true

The detail field in the RFC 9457 response identifies which header failed: Failed to negotiate content for: [Accept].

import {compose, accepts} from '@centralping/ergo';
const pipeline = compose(
accepts({types: ['application/json']}),
// acc.accepts.type === "application/json"
);

See the auto-generated accepts API docs.