ContentNegotiation¶
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 allAcceptheaders from aRequest valand 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:
- An absent Accept header means "accept anything" — the first supported type is returned.
- 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
MediaTypehas no parameters, so parameterized ranges don't match). - The supported type with the highest quality wins. Ties go to the first
type in the
supportedlist (server preference). - Types matched only by
q=0ranges are excluded. - If no supported type has quality > 0,
NoAcceptableTypeis returned.
Constructors¶
create¶
Returns¶
- ContentNegotiation val^
Public Functions¶
from_request¶
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¶
- (MediaType val | NoAcceptableType val)
apply¶
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¶
- (MediaType val | NoAcceptableType val)
eq¶
Parameters¶
- that: ContentNegotiation val
Returns¶
- Bool val
ne¶
Parameters¶
- that: ContentNegotiation val
Returns¶
- Bool val