When using session objects, deleting a key from headers fails #345

Closed
daVinci1980 opened this Issue Jan 10, 2012 · 2 comments

Projects

None yet

3 participants

@daVinci1980

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!

@kennethreitz
Owner

This is definitely a problem. Sorry for the bug!

@Lukasa
Collaborator
Lukasa commented Feb 15, 2012

From my messing about, this also happens if you aren't using sessions:

import requests
r = requests.get('http://httpbin.org/headers, headers={"foo":None})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "requests/sessions.py", line 217, in get
    return self.request('get', url, **kwargs)
  File "requests/sessions.py", line 159, in request
    headers[k] = header_expand(v)
  File "requests/utils.py", line 157, in header_expand
    for i, (value, params) in enumerate(headers):
TypeError: 'NoneType' object is not iterable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment