From 548a03acef3238389b1cce95131a0511d5c82211 Mon Sep 17 00:00:00 2001 From: Philipp Konrad Date: Thu, 27 Oct 2016 17:57:09 +0200 Subject: [PATCH] requests.post checked data parameters for type like dict. Changed the type check to Mapping. --- AUTHORS.rst | 3 ++- requests/models.py | 2 +- tests/test_requests.py | 25 +++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/AUTHORS.rst b/AUTHORS.rst index 1b4d3c2b..9e068eb8 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -170,4 +170,5 @@ Patches and Suggestions - Maik Himstedt - Michael Hunsinger - Brian Bamsch (`@bbamsch `_) -- Om Prakash Kumar (`@iamprakashom `_) \ No newline at end of file +- Om Prakash Kumar (`@iamprakashom `_) +- Philipp Konrad (`@gardiac2002 `_) diff --git a/requests/models.py b/requests/models.py index db9df865..ba791721 100644 --- a/requests/models.py +++ b/requests/models.py @@ -436,7 +436,7 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin): is_stream = all([ hasattr(data, '__iter__'), - not isinstance(data, (basestring, list, tuple, dict)) + not isinstance(data, (basestring, list, tuple, collections.Mapping)) ]) try: diff --git a/tests/test_requests.py b/tests/test_requests.py index f2d56b9e..5a2608e2 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -656,6 +656,31 @@ class TestRequests: with pytest.raises(ValueError): requests.post(url, files=['bad file data']) + def test_post_with_custom_mapping(self, httpbin): + class CustomMapping(collections.MutableMapping): + def __init__(self, *args, **kwargs): + self.data = dict(*args, **kwargs) + + def __delitem__(self, key): + del self.data[key] + + def __getitem__(self, key): + return self.data[key] + + def __setitem__(self, key, value): + self.data[key] = value + + def __iter__(self): + return iter(self.data) + + def __len__(self): + return len(self.data) + + data = CustomMapping({'some': 'data'}) + url = httpbin('post') + found_json = requests.post(url, data=data).json().get('form') + assert found_json == {'some': 'data'} + def test_conflicting_post_params(self, httpbin): url = httpbin('post') with open('requirements.txt') as f: