From 12328d6e58d0b52f0a5f425f4dcd84236583c679 Mon Sep 17 00:00:00 2001 From: Marcus McCurdy Date: Fri, 3 Aug 2012 20:09:56 -0400 Subject: [PATCH 1/4] Added a test to expose issue #747 --- tests/test_requests.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_requests.py b/tests/test_requests.py index 60a84077..10da6f7e 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -973,5 +973,12 @@ class RequestsTestSuite(TestSetup, TestBaseMixin, unittest.TestCase): self.assertEqual(t.get('form'), {'field': 'a, b'}) self.assertEqual(t.get('files'), files) + def test_str_data_content_type(self): + data = "test string data" + r = post(httpbin('post'), data=data) + t = json.loads(r.text) + self.assertEqual(t.get('headers').get('Content-Type'), 'text/plain') + + if __name__ == '__main__': unittest.main() From 2110f7d18c67ce42073b440df0853447df842e99 Mon Sep 17 00:00:00 2001 From: Marcus McCurdy Date: Fri, 3 Aug 2012 23:05:37 -0400 Subject: [PATCH 2/4] Fixed test for issue #747 to check for '' instead of 'text/plain' --- tests/test_requests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_requests.py b/tests/test_requests.py index 10da6f7e..928b23ef 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -977,7 +977,7 @@ class RequestsTestSuite(TestSetup, TestBaseMixin, unittest.TestCase): data = "test string data" r = post(httpbin('post'), data=data) t = json.loads(r.text) - self.assertEqual(t.get('headers').get('Content-Type'), 'text/plain') + self.assertEqual(t.get('headers').get('Content-Type'), '') if __name__ == '__main__': From 493ea934699d10f5e7fae174403c59fd511b605b Mon Sep 17 00:00:00 2001 From: Marcus McCurdy Date: Fri, 3 Aug 2012 23:13:17 -0400 Subject: [PATCH 3/4] Fixed issue #747 Bound the name builtin_str to __builtin__.str and added another check for this class since the str class was rebound to unicode in compat. This issue was only for Python 2 as well. --- requests/models.py | 4 +++- tests/test_requests.py | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/requests/models.py b/requests/models.py index 49007372..970acf8a 100644 --- a/requests/models.py +++ b/requests/models.py @@ -34,6 +34,8 @@ from .compat import ( cookielib, urlparse, urlunparse, urljoin, urlsplit, urlencode, str, bytes, StringIO, is_py2, chardet, json) +from __builtin__ import str as builtin_str + REDIRECT_STATI = (codes.moved, codes.found, codes.other, codes.temporary_moved) CONTENT_CHUNK_SIZE = 10 * 1024 @@ -493,7 +495,7 @@ class Request(object): if self.data: body = self._encode_params(self.data) - if isinstance(self.data, str) or hasattr(self.data, 'read'): + if isinstance(self.data, str) or isinstance(self.data, builtin_str) or hasattr(self.data, 'read'): content_type = None else: content_type = 'application/x-www-form-urlencoded' diff --git a/tests/test_requests.py b/tests/test_requests.py index 10da6f7e..9204b15d 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -974,10 +974,10 @@ class RequestsTestSuite(TestSetup, TestBaseMixin, unittest.TestCase): self.assertEqual(t.get('files'), files) def test_str_data_content_type(self): - data = "test string data" + data = 'test string data' r = post(httpbin('post'), data=data) t = json.loads(r.text) - self.assertEqual(t.get('headers').get('Content-Type'), 'text/plain') + self.assertEqual(t.get('headers').get('Content-Type'), '') if __name__ == '__main__': From f60579ddda183e10888bd5059ba3033792ddb043 Mon Sep 17 00:00:00 2001 From: Marcus McCurdy Date: Sat, 4 Aug 2012 09:35:32 -0400 Subject: [PATCH 4/4] Moved fix for #747 to compat module --- requests/compat.py | 2 ++ requests/models.py | 4 +--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/requests/compat.py b/requests/compat.py index 8e4a17e4..af0207de 100644 --- a/requests/compat.py +++ b/requests/compat.py @@ -91,6 +91,7 @@ if is_py2: from StringIO import StringIO from .packages import chardet + builtin_str = str bytes = str str = unicode basestring = basestring @@ -105,6 +106,7 @@ elif is_py3: from io import StringIO from .packages import chardet2 as chardet + builtin_str = str str = str bytes = bytes basestring = (str,bytes) diff --git a/requests/models.py b/requests/models.py index 970acf8a..0a83a681 100644 --- a/requests/models.py +++ b/requests/models.py @@ -32,9 +32,7 @@ from .utils import ( DEFAULT_CA_BUNDLE_PATH) from .compat import ( cookielib, urlparse, urlunparse, urljoin, urlsplit, urlencode, str, bytes, - StringIO, is_py2, chardet, json) - -from __builtin__ import str as builtin_str + StringIO, is_py2, chardet, json, builtin_str) REDIRECT_STATI = (codes.moved, codes.found, codes.other, codes.temporary_moved) CONTENT_CHUNK_SIZE = 10 * 1024