watson.http.headers¶
- class watson.http.headers.HeaderCollection(environ=None)[source]¶
Retrieves header related variables from an environ.
Allows the use of non-capitalized names.
Example:
headers = HeaderCollection.from_environ(environ) print(headers.get('Content-Type'))
- add(field, value, replace=False, **options)[source]¶
Add a header to the collection.
Parameters: - field (string) – The field name
- value (mixed) – The value of the field
- replace (boolean) – Whether or not to replace an existing field
- options (kwargs) – Any additional options for the header
Example:
headers = ... headers.add('Content-Type', 'text/html', charset='utf-8')
- get(field, option=None, default=None)[source]¶
Retrieve an individual header or it’s option.
Example:
# Content-Type: text/html; charset=utf-8 headers = HeaderCollection() headers.add('Content-Type', 'text/html', charset='utf-8') option = headers.get('Content-Type', 'charset') # utf-8
Parameters: - field – the header field
- option – the option to retrieve from the field
- default – the default value if the option does not exist
Returns: The default value or the value from the option
- get_option(field, option=None, default=None)¶
Retrieve an individual header or it’s option.
Example:
# Content-Type: text/html; charset=utf-8 headers = HeaderCollection() headers.add('Content-Type', 'text/html', charset='utf-8') option = headers.get('Content-Type', 'charset') # utf-8
Parameters: - field – the header field
- option – the option to retrieve from the field
- default – the default value if the option does not exist
Returns: The default value or the value from the option
- set(field, value, **options)[source]¶
Add a header to the collection.
Any existing headers with the same name will be removed.
Parameters: - field (string) – The field name
- value (mixed) – The value of the field
- options (kwargs) – Any additional options for the header
Example:
headers = ... headers.add('Content-Type', 'text/html', charset='utf-8')
- class watson.http.headers.ServerCollection(environ=None)[source]¶
Retrieves server related variables from an environ.
Example:
server = ServerCollection(environ) print(server['SCRIPT_NAME'])
- watson.http.headers.convert_to_http_field(field)[source]¶
Convert a field from Title-Case to HTTP_UPPER_CASE.