Skip to content

ContentNegotiation

[Source]

Select the best response content type based on the client's Accept header preferences (RFC 7231 §5.3.2).

This is an opt-in utility — most endpoints serve a single content type, so automatic parsing of every request's Accept header would waste CPU. Call from_request() or apply() only in handlers that support multiple content types.

Two entry points:

  • from_request() — extracts all Accept headers from a Request val and negotiates against the server's supported types.
  • apply() — negotiates directly from a raw Accept header value string. Useful for testing or when you already have the header value.

The algorithm follows RFC 7231 §5.3.2 precedence rules:

  1. An absent Accept header means "accept anything" — the first supported type is returned.
  2. Each supported type is matched against the most specific compatible range in the Accept header. Ranges with media parameters only match types with matching parameters (but MediaType has no parameters, so parameterized ranges don't match).
  3. The supported type with the highest quality wins. Ties go to the first type in the supported list (server preference).
  4. Types matched only by q=0 ranges are excluded.
  5. If no supported type has quality > 0, NoAcceptableType is returned.
primitive val ContentNegotiation

Constructors

create

[Source]

new val create()
: ContentNegotiation val^

Returns


Public Functions

from_request

[Source]

Negotiate content type from a request's Accept headers.

Multiple Accept headers are concatenated, matching RFC 7230 §3.2.2 semantics for list-based header fields.

fun box from_request(
  request': Request val,
  supported: ReadSeq[MediaType val] box)
: (MediaType val | NoAcceptableType val)

Parameters

Returns


apply

[Source]

Negotiate content type from a raw Accept header value string.

Empty supported always returns NoAcceptableType. An empty header_value means "accept anything" — the first supported type is returned.

fun box apply(
  header_value: String val,
  supported: ReadSeq[MediaType val] box)
: (MediaType val | NoAcceptableType val)

Parameters

Returns


eq

[Source]

fun box eq(
  that: ContentNegotiation val)
: Bool val

Parameters

Returns


ne

[Source]

fun box ne(
  that: ContentNegotiation val)
: Bool val

Parameters

Returns