5. Headers

5.1. Synopsis

>>> from http import Headers
>>> headers = Headers()
>>> headers = Headers([('Content-Type', 'application/json')])

5.2. Interface

class http.headers.Headers([defaults])

Class to manipulate HTTP headers

add(key, *values)

Adds a new header and one or multiple values

Parameters:
  • key (string) – name of the header
  • *values – one or many values for this header
content_is_json

Returns True if the “Content-Type” header is set to application/json

Return type:boolean
content_is_text

Returns True if the “Content-Type” header is set to text

Return type:boolean
content_is_xhtml

Returns True if the “Content-Type” header is set to xhtml

Return type:boolean
content_is_xml

Returns True if the “Content-Type” header is set to xml

Return type:boolean
content_length

Returns the value for the Content-Length header

Return type:int
content_type

Returns the value for the Content-Type header

Return type:string
content_type_params

Returns the type parameters for the Content-Type header

Return type:dictionary
date

Returns a datetime object represeting the Date header

Return type:datetime
expires

Returns a datetime object represeting the Expires header

Return type:datetime
get(key)

Returns the value for a given header

Parameters:key (string) – name of the header
Return type:string
get_all(key)

Returns all the values for a given header

Parameters:key (string) – name of the header
Return type:list
get_list(key)
if_modified_since

Returns a datetime object represeting the If-Modified-Since header

Return type:datetime
if_unmodified_since

Returns a datetime object represeting the If-Unmodified-Since header

Return type:datetime
items()

Returns a list of items

Return type:list
iteritems()

Returns an iterator

Return type:iterator
last_modified

Returns a datetime object represeting the Last-Modified header

Return type:datetime
remove(key)

Removes a header

Parameters:key (string) – name of the header
set(key, value)

Set a header to some specific value. If this header already exists, and there is more than one value for this header, the new value replace the first one

Parameters:
  • key (string) – name of the header
  • value – new value
to_list()