From 37037607b5f3da32ff1897caca3c38e210f04c37 Mon Sep 17 00:00:00 2001 From: Ravi Prakash Putchala Date: Tue, 8 Sep 2015 14:45:46 +0530 Subject: [PATCH 1/3] Fixed issue #2756 Now 'json' parameter will be used to prepare body only if the 'data' parameter is not present --- requests/models.py | 4 ++-- test_requests.py | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/requests/models.py b/requests/models.py index 4270c647..9fa367d3 100644 --- a/requests/models.py +++ b/requests/models.py @@ -414,7 +414,7 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin): content_type = None length = None - if json is not None: + if not data and json is not None: content_type = 'application/json' body = complexjson.dumps(json) @@ -443,7 +443,7 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin): if files: (body, content_type) = self._encode_files(files, data) else: - if data and json is None: + if data: body = self._encode_params(data) if isinstance(data, basestring) or hasattr(data, 'read'): content_type = None diff --git a/test_requests.py b/test_requests.py index 28ea5730..fedf7293 100755 --- a/test_requests.py +++ b/test_requests.py @@ -1062,6 +1062,13 @@ class RequestsTestCase(unittest.TestCase): assert 'application/json' in r.request.headers['Content-Type'] assert {'life': 42} == r.json()['json'] + def test_json_param_post_should_not_override_data_param(self): + r = requests.Request(method='POST', url='http://httpbin.org/post', + data={'stuff'.encode('utf-8'): 'elixr'}, + json={'music'.encode('utf-8'): 'flute'}) + prep = r.prepare() + assert 'stuff=elixr' == prep.body + def test_response_iter_lines(self): r = requests.get(httpbin('stream/4'), stream=True) assert r.status_code == 200 From 7a0cd16c1a5be16f3df395ee12ed11d0ddde7837 Mon Sep 17 00:00:00 2001 From: Ravi Prakash Putchala Date: Wed, 9 Sep 2015 13:04:24 +0530 Subject: [PATCH 2/3] changed the null check for 'data' parameter to be more explicit. removed the superfluous encode from the testcase. --- requests/models.py | 2 +- test_requests.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/requests/models.py b/requests/models.py index 9fa367d3..06e84362 100644 --- a/requests/models.py +++ b/requests/models.py @@ -414,7 +414,7 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin): content_type = None length = None - if not data and json is not None: + if data == {} and json is not None: content_type = 'application/json' body = complexjson.dumps(json) diff --git a/test_requests.py b/test_requests.py index fedf7293..87b47a19 100755 --- a/test_requests.py +++ b/test_requests.py @@ -1065,7 +1065,7 @@ class RequestsTestCase(unittest.TestCase): def test_json_param_post_should_not_override_data_param(self): r = requests.Request(method='POST', url='http://httpbin.org/post', data={'stuff'.encode('utf-8'): 'elixr'}, - json={'music'.encode('utf-8'): 'flute'}) + json={'music': 'flute'}) prep = r.prepare() assert 'stuff=elixr' == prep.body From 1f48a4c9ea81d6b2128a7e1199be2dfbb81b643d Mon Sep 17 00:00:00 2001 From: Ravi Prakash Putchala Date: Wed, 9 Sep 2015 14:43:22 +0530 Subject: [PATCH 3/3] Removed the other 'encode' as well, in the test case. --- test_requests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_requests.py b/test_requests.py index 87b47a19..b5be37fc 100755 --- a/test_requests.py +++ b/test_requests.py @@ -1064,7 +1064,7 @@ class RequestsTestCase(unittest.TestCase): def test_json_param_post_should_not_override_data_param(self): r = requests.Request(method='POST', url='http://httpbin.org/post', - data={'stuff'.encode('utf-8'): 'elixr'}, + data={'stuff': 'elixr'}, json={'music': 'flute'}) prep = r.prepare() assert 'stuff=elixr' == prep.body