kennethreitz/requests — Issue #424: Unicode header dict values cause unhandled exception
Author: foxx
Created: 2012-02-13 21:27:39+00:00
State: open

Hey,

The following problem happens when you use a unicode string for a header dict:

```
_headers = {
'test' : u'hello world'
}

requests.get('http://example.com', headers = _headers)

File "/home/foxx/dev/iz/webapp/../webapp/libs/_requests/utils.py", line 152, in header_expand
for i, (value, params) in enumerate(headers):
ValueError: need more than 1 value to unpack
```

This is due to the following code:

```

def header_expand(headers):
"""Returns an HTTP Header value string from a dictionary.

Example expansion::

{'text/x-dvi': {'q': '.8', 'mxb': '100000', 'mxt': '5.0'}, 'text/x-c': {}}
# Accept: text/x-dvi; q=.8; mxb=100000; mxt=5.0, text/x-c

(('text/x-dvi', {'q': '.8', 'mxb': '100000', 'mxt': '5.0'}), ('text/x-c', {}))
# Accept: text/x-dvi; q=.8; mxb=100000; mxt=5.0, text/x-c
"""

collector = []

if isinstance(headers, dict):
headers = list(headers.items())

elif isinstance(headers, str):
return headers
```

We have hotfix'd this by doing:

```
elif isinstance(headers, str) or isinstance(headers, unicode):
return headers
```

Hope this helps!

Cal