Skip to content

ietfparse.algorithms

ietfparse.algorithms.select_content_type(requested, available, *, default=None)

Select the best content type.

This function implements the Proactive Content Negotiation algorithm as described in RFC-9110. The input is the Accept header as parsed by ietfparse.headers.parse_accept and a list of parsed ietfparse.datastructures.ContentType instances. The available sequence should be a sequence of content types that the server is capable of producing. The selected value should ultimately be used as the Content-Type header in the generated response.

Parameters:

Name Type Description Default
requested Sequence[ContentType | str] | str | None

a sequence of ietfparse.datastructures.ContentType instances

required
available Sequence[ContentType | str]

a sequence of ietfparse.datastructures.ContentType instances that the server is capable of producing

required
default ContentType | str | None

optional default value to return if there is no acceptable match

None

Returns:

Type Description
tuple[ContentType, ContentType]

the selected content type (from available) and the pattern that it matched (from requested)

Raises:

Type Description
ietfparse.errors.NoMatch

when a suitable match was not found

ValueError

when default is specified and it is not in available