Source API¶
HeaderParser¶
- class parser_header.HeaderParser(data=None, **kwargs)[source]¶
Bases:
objectComprehensive HTTP header parser supporting all standard and custom headers.
Initialize HeaderParser.
- Parameters:
data (Optional[InputType]) – Raw header string/bytes
**kwargs (Any) – Header key-value pairs to set directly Underscores in keys are converted to hyphens e.g., HeaderParser(content_type=”application/json”, user_agent=”Mozilla”)
- SPECIAL_HEADERS = {'accept', 'authorization', 'cache-control', 'content-disposition', 'content-type', 'cookie', 'proxy-authenticate', 'set-cookie', 'www-authenticate'}¶
- BOOLEAN_HEADERS = {'dnt', 'sec-gpc', 'upgrade-insecure-requests'}¶
- SEC_CH_UA_PATTERN = re.compile('"([^"]+)";v="([^"]+)"')¶
- set(name=None, value=None, **kwargs)[source]¶
Set header(s).
- Parameters:
- Returns:
self for chaining
- Return type:
Examples
parser.set(‘Content-Type’, ‘application/json’) parser.set(content_type=’application/json’, user_agent=’Mozilla’) parser.set(‘X-Custom’, ‘value’, accept=’/’)
- set_raw(name=None, value=None, **kwargs)[source]¶
Set header(s) without parsing the value.
- Parameters:
- Returns:
self for chaining
- Return type:
- update(data=None, **kwargs)[source]¶
Update headers from dict and/or kwargs.
- Parameters:
- Returns:
self for chaining
- Return type:
- to_dict(stringify=False)[source]¶
Get all headers as dictionary.
- Parameters:
stringify (bool) – If True, convert all values to strings
- property cookies: CookieParser¶
Access cookie parser.
HeaderValue¶
Exceptions¶
- class parser_header.exceptions.ParserError[source]¶
Bases:
ExceptionBase exception for parser errors.
- class parser_header.exceptions.InvalidHeaderError[source]¶
Bases:
ParserErrorRaised when header format is invalid.
- class parser_header.exceptions.InvalidCookieError[source]¶
Bases:
ParserErrorRaised when cookie format is invalid.
- class parser_header.exceptions.EncodingError[source]¶
Bases:
ParserErrorRaised when encoding/decoding fails.