Skip to main content

MediaType

This class parses and represents a media type string, including its main type, sub-type, and optional parameters. It provides functionality to match media types against each other using wildcard logic and evaluates the specificity and quality of the media type. The class also supports reconstructing the media type into a standardized string format.

Attributes

AttributeTypeDescription
paramsdictA dictionary of key-value pairs representing the optional parameters parsed from the media type header line.
main_typestringThe primary category of the media type, such as 'text' or 'application', extracted from the portion before the slash.
sub_typestringThe specific format of the media type, such as 'html' or 'json', extracted from the portion after the slash.
range_paramsdictA copy of the media type parameters excluding the 'q' quality factor, used for matching media ranges.
qualityfloat = 1The relative preference weight of the media type, parsed from the 'q' parameter as a float between 0 and 1.
specificityintegerReturn a value from 0-3 for how specific the media type is.

Constructor

Signature

def MediaType(
media_type_raw_line: str
) - > null

Parameters

NameTypeDescription
media_type_raw_linestrThe raw media type string to be parsed.

Methods


range_params()

@classmethod
def range_params() - > dict

Extracts all media type parameters excluding the 'q' (quality) parameter to isolate content-specific attributes.

Returns

TypeDescription
dictA dictionary of parameters used for matching, with the quality factor removed.

match()

@classmethod
def match(
other: [Union](../../contrib/gis/db/models/functions/union.md?sid=django_contrib_gis_db_models_functions_union)[[MediaType](mediatype.md?sid=django_http_request_mediatype), str]
) - > boolean

Determines if this media type is compatible with another media type, accounting for wildcards and parameter subsets.

Parameters

NameTypeDescription
other[Union](../../contrib/gis/db/models/functions/union.md?sid=django_contrib_gis_db_models_functions_union)[[MediaType](mediatype.md?sid=django_http_request_mediatype), str]The media type instance or raw string to compare against the current instance.

Returns

TypeDescription
booleanTrue if the types, sub-types, and parameters are compatible according to content negotiation rules; False otherwise.

quality()

@classmethod
def quality() - > float

Parses and returns the 'q' parameter value to determine the preference weight of the media type.

Returns

TypeDescription
floatA float between 0 and 1 representing the quality factor; defaults to 1 if the parameter is missing or invalid.

specificity()

@classmethod
def specificity() - > integer

Return a value from 0-3 for how specific the media type is.

Returns

TypeDescription
integerAn integer where 0 is a catch-all wildcard, 1 is a type wildcard, 2 is a specific type/subtype, and 3 includes parameters.