Encode both keys and values for incoming unicode data as UTF-8, coincidentally fixing a bug that caused UTF-8 encoded byte strings to be encoded twice and causing an UnicodeDecodeError.

This commit is contained in:
Johannes Gorset
2011-05-12 10:16:21 +02:00
parent 59dd3ed459
commit 23ee58d6ec
2 changed files with 9 additions and 4 deletions
+7 -4
View File
@@ -70,13 +70,16 @@ class Request(object):
self.headers = headers
self.files = files
self.method = method
self.data = data
self.data = {}
for (k, v) in data.items():
self.data.update({
k.encode('utf-8') if k.__class__ is unicode else k: \
v.encode('utf-8') if v.__class__ is unicode else v
})
socket.setdefaulttimeout(timeout)
for (k, v) in self.data.iteritems():
self.data[k] = v.encode('utf-8')
# url encode data if it's a dict
if hasattr(data, 'items'):
self._enc_data = urllib.urlencode(self.data)
+2
View File
@@ -141,6 +141,8 @@ class RequestsTestSuite(unittest.TestCase):
def test_unicode_get(self):
requests.get('http://google.com', params={'foo': u'føø'})
requests.get('http://google.com', params={u'føø': u'føø'})
requests.get('http://google.com', params={'føø': 'føø'})
requests.get('http://google.com', params={'foo': u'foo'})
requests.get('http://google.com/ø', params={'foo': u'foo'})