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";
OptionTypeDefaultDescription
throwIfFailbooleantrueReturn 406 when any negotiated value is undefined
typesstring[]Acceptable media types
languagesstring[]Acceptable languages
charsetsstring[]Acceptable character sets
encodingsstring[]Acceptable content encodings

On success, the middleware returns a negotiation result stored 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.

StatusCondition
406 Not AcceptableAny 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(
{fn: accepts({ types: ["application/json"] }), setPath: "accepts"},
// acc.accepts.type === "application/json"
);

See the auto-generated accepts API docs.