werkzeug.Headers
¶
-
class
werkzeug.
Headers
(defaults=None)[source]¶ An object that stores some headers. It has a dict-like interface but is ordered and can store the same keys multiple times.
This data structure is useful if you want a nicer way to handle WSGI headers which are stored as tuples in a list.
From Werkzeug 0.3 onwards, the
KeyError
raised by this class is also a subclass of theBadRequest
HTTP exception and will render a page for a400 BAD REQUEST
if caught in a catch-all for HTTP exceptions.Headers is mostly compatible with the Python
wsgiref.headers.Headers
class, with the exception of __getitem__.wsgiref
will return None forheaders['missing']
, whereasHeaders
will raise aKeyError
.To create a new
Headers
object pass it a list or dict of headers which are used as default values. This does not reuse the list passed to the constructor for internal usage.Parameters: defaults – The list of default values for the Headers
.Changed in version 0.9: This data structure now stores unicode values similar to how the multi dicts do it. The main difference is that bytes can be set as well which will automatically be latin1 decoded.
Changed in version 0.9: The
linked()
function was removed without replacement as it was an API that does not support the changes to the encoding model.
Methods¶
__init__ ([defaults]) |
|
add (_key, _value, **kw) |
Add a new header tuple to the list. |
add_header (_key, _value, **_kw) |
Add a new header tuple to the list. |
clear () |
Clears all headers. |
copy () |
|
extend (iterable) |
Extend the headers with a dict or an iterable yielding keys and values. |
get (key[, default, type, as_bytes]) |
Return the default value if the requested data doesn’t exist. |
get_all (name) |
Return a list of all the values for the named field. |
getlist (key[, type, as_bytes]) |
Return the list of items for a given key. |
has_key (key) |
Check if a key is present. |
items (*a, **kw) |
Like iteritems() , but returns a list. |
iteritems ([lower]) |
|
iterkeys ([lower]) |
|
itervalues () |
|
keys (*a, **kw) |
Like iterkeys() , but returns a list. |
pop ([key, default]) |
Removes and returns a key or index. |
popitem () |
Removes a key or index and returns a (key, value) item. |
remove (key) |
Remove a key. |
set (_key, _value, **kw) |
Remove all header tuples for key and add a new one. |
setdefault (key, value) |
Returns the value for the key if it is in the dict, otherwise it returns default and sets that value for key. |
to_list ([charset]) |
Convert the headers into a list suitable for WSGI. |
to_wsgi_list () |
Convert the headers into a list suitable for WSGI. |
values (*a, **kw) |
Like itervalues() , but returns a list. |