Unicode cookie key values fail in translate

Hi,

the following code

```
import requests

cookie = {u'test_cookie': 'foobar'}
res = requests.get('http://httpbin.org/cookies', cookies=cookie)
print res.cookies
```

thows the following error

``` Python
  File "requests_translate_bug.py", line 5, in <module>
    res = requests.get('http://httpbin.org/cookies', cookies=cookie)
  File "/home/markl/Envs/req-bug/lib/python2.6/site-packages/requests/api.py", line 51, in get
    return request('get', url, **kwargs)
  File "/home/markl/Envs/req-bug/lib/python2.6/site-packages/requests/api.py", line 39, in request
    return s.request(method=method, url=url, **kwargs)
  File "/home/markl/Envs/req-bug/lib/python2.6/site-packages/requests/sessions.py", line 200, in request
    r.send(prefetch=prefetch)
  File "/home/markl/Envs/req-bug/lib/python2.6/site-packages/requests/models.py", line 507, in send
    c[k] = v
  File "/home/markl/Envs/req-bug/lib/python2.6/site-packages/requests/packages/oreos/monkeys.py", line 604, in __setitem__
    self.__set(key, rval, cval)
  File "/home/markl/Envs/req-bug/lib/python2.6/site-packages/requests/packages/oreos/monkeys.py", line 597, in __set
    M.set(key, real_value, coded_value)
  File "/home/markl/Envs/req-bug/lib/python2.6/site-packages/requests/packages/oreos/monkeys.py", line 470, in set
    if "" != translate(key, idmap, LegalChars):
  File "/usr/lib/python2.6/string.py", line 493, in translate
    return s.translate(table, deletions)
TypeError: translate() takes exactly one argument (2 given)

```

If the key in the cookies dictionary isnt a unicode character then this doesnt happen.

(req-bug)markl@markl-desktop:~/dev/requests-bug$ pip freeze
certifi==0.0.8
chardet==1.0.1
requests==0.10.6
wsgiref==0.1.2
(req-bug)markl@markl-desktop:~/dev/requests-bug$ 

Hope it helps.

Cheers

Mark


This is related to #536.  The root cause is that HTML headers only support Latin-1 encoding, so unicode is not allowed in either the key or the data within a cookie.  It appears that httpbin is happy to ignore this, and so it is sending back invalid cookies with unicode encoding.

The correct solution is to urlencode the contents of a cookie before setting, but ideally this would be done before it's passed in to requests.

Any thoughts on whether it makes sense to try to handle this case within requests?  Or, would it just be better to throw an error that is more descriptive?


I can see where you coming from in regards to the HTML spec and Latin-1 encoding. I guess the risks as far as requests fixing a unicode key is that its unable to translate it to Latin-1 for some reason and then the request breaks which could be much harder to track down.

Probably a better error would be the best way and possibly a check up front in terms of the request?


Original test case no longer crashes:

```
>>> import requests
>>> cookie = {u'test_cookie': 'foobar'}
>>> res = requests.get('http://httpbin.org/cookies', cookies=cookie)
>>> print res.cookies
<<class 'requests.cookies.RequestsCookieJar'>[<Cookie test_cookie=foobar for />]>
>>> cookie = list(res.cookies)[0]
>>> cookie.name, cookie.value
(u'test_cookie', 'foobar')
```

I think this is probably resolved, since `cookielib` is taking care of unicode-to-bytes now.


\o/