kennethreitz/requests — Issue #345: When using session objects, deleting a key from headers fails
Author: daVinci1980
Created: 2012-01-10 07:12:35+00:00
State: open

Using version 0.91, downloaded 1/09/12, this code produces the specified exception:

>>> import requests
>>> s = requests.session(headers={"foo": "bar"})
>>> s.get('http://httpbin.org/headers', headers={"foo": None})
Traceback (most recent call last):
File "", line 1, in
File "C:\Python27\lib\site-packages\requests\sessions.py", line 217, in get
return self.request('get', url, **kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 159, in request
headers[k] = header_expand(v)
File "C:\Python27\lib\site-packages\requests\utils.py", line 140, in header_expand
for i, (value, params) in enumerate(headers):
TypeError: 'NoneType' object is not iterable

The problem is that expand_header, in requests/utils.py:140, because "None" is not enumerable.

The fix I've patched in is to add 'elif headers is None: return headers' before enumerating the headers variable (part of the prior if/elif block).

Great library, thanks!