class documentation

An object that stores some headers. It has a dict-like interface, but is ordered, can store the same key multiple times, and iterating yields ``(key, value)`` pairs instead of only keys. 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 :exc:`KeyError` raised by this class is also a subclass of the :class:`~exceptions.BadRequest` HTTP exception and will render a page for a ``400 BAD REQUEST`` if caught in a catch-all for HTTP exceptions. Headers is mostly compatible with the Python :class:`wsgiref.headers.Headers` class, with the exception of `__getitem__`. :mod:`wsgiref` will return `None` for ``headers['missing']``, whereas :class:`Headers` will raise a :class:`KeyError`. To create a new ``Headers`` object, pass it a list, dict, or other ``Headers`` object with default values. These values are validated the same way values added later are. :param defaults: The list of default values for the :class:`Headers`. .. versionchanged:: 2.1.0 Default values are validated the same as values added later. .. versionchanged:: 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. .. versionchanged:: 0.9 The :meth:`linked` function was removed without replacement as it was an API that does not support the changes to the encoding model.

Method __contains__ Check if a key is present.
Method __copy__ Undocumented
Method __delitem__ Undocumented
Method __eq__ Undocumented
Method __getitem__ Undocumented
Method __init__ Undocumented
Method __iter__ Yield ``(key, value)`` tuples.
Method __len__ Undocumented
Method __repr__ Undocumented
Method __setitem__ Like :meth:`set` but also supports index/slice based setting.
Method __str__ Returns formatted headers suitable for HTTP transmission.
Method add Add a new header tuple to the list.
Method add_header Add a new header tuple to the list.
Method clear Clears all headers.
Method copy Undocumented
Method extend Extend headers in this object with items from another object containing header items as well as keyword arguments.
Method get Return the default value if the requested data doesn't exist. If `type` is provided and is a callable it should convert the value, return it or raise a :exc:`ValueError` if that is not possible. In this case the function will return the default as if the value was not found:...
Method get_all Return a list of all the values for the named field.
Method getlist Return the list of items for a given key. If that key is not in the :class:`Headers`, the return value will be an empty list. Just like :meth:`get`, :meth:`getlist` accepts a `type` parameter. All items will be converted with the callable defined there.
Method items Undocumented
Method keys Undocumented
Method pop Removes and returns a key or index.
Method popitem Removes a key or index and returns a (key, value) item.
Method remove Remove a key.
Method set Remove all header tuples for `key` and add a new one. The newly added key either appears at the end of the list if there was no entry or replaces the first one.
Method setdefault Return the first value for the key if it is in the headers, otherwise set the header to the value given by ``default`` and return that.
Method setlist Remove any existing values for a header and add new ones.
Method setlistdefault Return the list of values for the key if it is in the headers, otherwise set the header to the list of values given by ``default`` and return that.
Method to_wsgi_list Convert the headers into a list suitable for WSGI.
Method update Replace headers in this object with items from another headers object and keyword arguments.
Method values Undocumented
Class Variable __hash__ Undocumented
Method _validate_value Undocumented
Instance Variable _list Undocumented
def __contains__(self, key): (source)

Check if a key is present.

def __copy__(self): (source)

Undocumented

def __delitem__(self, key, _index_operation=True): (source)

Undocumented

def __eq__(self, other): (source)

Undocumented

def __getitem__(self, key, _get_mode=False): (source)

Undocumented

def __init__(self, defaults=None): (source)

Undocumented

def __iter__(self): (source)

Yield ``(key, value)`` tuples.

def __len__(self): (source)

Undocumented

def __repr__(self): (source)

Undocumented

def __setitem__(self, key, value): (source)

Like :meth:`set` but also supports index/slice based setting.

def __str__(self): (source)

Returns formatted headers suitable for HTTP transmission.

def add(self, _key, _value, **kw): (source)

Add a new header tuple to the list. Keyword arguments can specify additional parameters for the header value, with underscores converted to dashes:: >>> d = Headers() >>> d.add('Content-Type', 'text/plain') >>> d.add('Content-Disposition', 'attachment', filename='foo.png') The keyword argument dumping uses :func:`dump_options_header` behind the scenes. .. versionadded:: 0.4.1 keyword arguments were added for :mod:`wsgiref` compatibility.

def add_header(self, _key, _value, **_kw): (source)

Add a new header tuple to the list. An alias for :meth:`add` for compatibility with the :mod:`wsgiref` :meth:`~wsgiref.headers.Headers.add_header` method.

def clear(self): (source)

Clears all headers.

def copy(self): (source)

Undocumented

def extend(self, *args, **kwargs): (source)

Extend headers in this object with items from another object containing header items as well as keyword arguments. To replace existing keys instead of extending, use :meth:`update` instead. If provided, the first argument can be another :class:`Headers` object, a :class:`MultiDict`, :class:`dict`, or iterable of pairs. .. versionchanged:: 1.0 Support :class:`MultiDict`. Allow passing ``kwargs``.

def get(self, key, default=None, type=None, as_bytes=False): (source)

Return the default value if the requested data doesn't exist. If `type` is provided and is a callable it should convert the value, return it or raise a :exc:`ValueError` if that is not possible. In this case the function will return the default as if the value was not found: >>> d = Headers([('Content-Length', '42')]) >>> d.get('Content-Length', type=int) 42 .. versionadded:: 0.9 Added support for `as_bytes`. :param key: The key to be looked up. :param default: The default value to be returned if the key can't be looked up. If not further specified `None` is returned. :param type: A callable that is used to cast the value in the :class:`Headers`. If a :exc:`ValueError` is raised by this callable the default value is returned. :param as_bytes: return bytes instead of strings.

def get_all(self, name): (source)

Return a list of all the values for the named field. This method is compatible with the :mod:`wsgiref` :meth:`~wsgiref.headers.Headers.get_all` method.

def getlist(self, key, type=None, as_bytes=False): (source)

Return the list of items for a given key. If that key is not in the :class:`Headers`, the return value will be an empty list. Just like :meth:`get`, :meth:`getlist` accepts a `type` parameter. All items will be converted with the callable defined there. .. versionadded:: 0.9 Added support for `as_bytes`. :param key: The key to be looked up. :param type: A callable that is used to cast the value in the :class:`Headers`. If a :exc:`ValueError` is raised by this callable the value will be removed from the list. :return: a :class:`list` of all the values for the key. :param as_bytes: return bytes instead of strings.

def items(self, lower=False): (source)

Undocumented

def keys(self, lower=False): (source)

Undocumented

def pop(self, key=None, default=_missing): (source)

Removes and returns a key or index. :param key: The key to be popped. If this is an integer the item at that position is removed, if it's a string the value for that key is. If the key is omitted or `None` the last item is removed. :return: an item.

def popitem(self): (source)

Removes a key or index and returns a (key, value) item.

def remove(self, key): (source)

Remove a key. :param key: The key to be removed.

def set(self, _key, _value, **kw): (source)

Remove all header tuples for `key` and add a new one. The newly added key either appears at the end of the list if there was no entry or replaces the first one. Keyword arguments can specify additional parameters for the header value, with underscores converted to dashes. See :meth:`add` for more information. .. versionchanged:: 0.6.1 :meth:`set` now accepts the same arguments as :meth:`add`. :param key: The key to be inserted. :param value: The value to be inserted.

def setdefault(self, key, default): (source)

Return the first value for the key if it is in the headers, otherwise set the header to the value given by ``default`` and return that. :param key: The header key to get. :param default: The value to set for the key if it is not in the headers.

def setlist(self, key, values): (source)

Remove any existing values for a header and add new ones. :param key: The header key to set. :param values: An iterable of values to set for the key. .. versionadded:: 1.0

def setlistdefault(self, key, default): (source)

Return the list of values for the key if it is in the headers, otherwise set the header to the list of values given by ``default`` and return that. Unlike :meth:`MultiDict.setlistdefault`, modifying the returned list will not affect the headers. :param key: The header key to get. :param default: An iterable of values to set for the key if it is not in the headers. .. versionadded:: 1.0

def to_wsgi_list(self): (source)

Convert the headers into a list suitable for WSGI. :return: list

def update(self, *args, **kwargs): (source)

Replace headers in this object with items from another headers object and keyword arguments. To extend existing keys instead of replacing, use :meth:`extend` instead. If provided, the first argument can be another :class:`Headers` object, a :class:`MultiDict`, :class:`dict`, or iterable of pairs. .. versionadded:: 1.0

def values(self): (source)

Undocumented

__hash__ = (source)

Undocumented

def _validate_value(self, value): (source)

Undocumented

Undocumented