From 691e9520ed1ed7d1d686f2fa1cc76a1d5c24e335 Mon Sep 17 00:00:00 2001 From: Antti Kaihola Date: Wed, 20 Jun 2018 22:12:10 +0300 Subject: [PATCH 1/4] Add test for multivalued form-encoded element as a list (#4700) --- tests/test_requests.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/test_requests.py b/tests/test_requests.py index dea411bf..fd04ad27 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -868,6 +868,12 @@ class TestRequests: assert r.status_code == 200 assert r.url == httpbin('get?test=foo&test=baz') + def test_form_encoded_post_query_multivalued_element(self, httpbin): + r = requests.Request(method='POST', url=httpbin('post'), + data=dict(test=['foo', 'baz'])) + prep = r.prepare() + assert prep.body == 'test=foo&test=baz' + def test_different_encodings_dont_break_post(self, httpbin): r = requests.post(httpbin('post'), data={'stuff': json.dumps({'a': 123})}, From 9a8eb131c9cba816d2835b3dcdb1359922b8018d Mon Sep 17 00:00:00 2001 From: Antti Kaihola Date: Wed, 20 Jun 2018 22:20:00 +0300 Subject: [PATCH 2/4] Documentation for multivalued form-encoded element as a list (#4700) --- docs/user/quickstart.rst | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/docs/user/quickstart.rst b/docs/user/quickstart.rst index 952f447d..d5e3fad1 100644 --- a/docs/user/quickstart.rst +++ b/docs/user/quickstart.rst @@ -261,6 +261,23 @@ useful when the form has multiple elements that use the same key:: ... } +An alternative method to express multiple elements with the same key is to use +a dictionary and list the element values as a list:: + + >>> payload = {'key1': ['value1', 'value2']} + >>> r = requests.post('http://httpbin.org/post', data=payload) + >>> print(r.text) + { + ... + "form": { + "key1": [ + "value1", + "value2" + ] + }, + ... + } + There are times that you may want to send data that is not form-encoded. If you pass in a ``string`` instead of a ``dict``, that data will be posted directly. From 87fb312534fda5aa7996d5172e726c0872cd042e Mon Sep 17 00:00:00 2001 From: Antti Kaihola Date: Wed, 20 Jun 2018 22:25:05 +0300 Subject: [PATCH 3/4] Add myself to the authors list --- AUTHORS.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS.rst b/AUTHORS.rst index c915e851..9f22c9e2 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -188,3 +188,4 @@ Patches and Suggestions - Demetrios Bairaktaris (`@DemetriosBairaktaris `_) - Darren Dormer (`@ddormer `_) - Rajiv Mayani (`@mayani `_) +- Antti Kaihola (`@akaihola `_) From 9c92f757e6ced45d494a8e174fa6dd2ef27ca70a Mon Sep 17 00:00:00 2001 From: Antti Kaihola Date: Fri, 29 Jun 2018 21:09:16 +0300 Subject: [PATCH 4/4] Rolled multi-value form examples in documentation into one (#4700) As suggested by @nateprewitt --- docs/user/quickstart.rst | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/docs/user/quickstart.rst b/docs/user/quickstart.rst index d5e3fad1..035b94d2 100644 --- a/docs/user/quickstart.rst +++ b/docs/user/quickstart.rst @@ -244,29 +244,16 @@ dictionary of data will automatically be form-encoded when the request is made:: ... } -You can also pass a list of tuples to the ``data`` argument. This is particularly -useful when the form has multiple elements that use the same key:: +The ``data`` argument can also have multiple values for each key. This can be +done by making ``data`` either a list of tuples or a dictionary with lists +as values. This is particularly useful when the form has multiple elements that +use the same key:: - >>> payload = (('key1', 'value1'), ('key1', 'value2')) - >>> r = requests.post('http://httpbin.org/post', data=payload) - >>> print(r.text) - { - ... - "form": { - "key1": [ - "value1", - "value2" - ] - }, - ... - } - -An alternative method to express multiple elements with the same key is to use -a dictionary and list the element values as a list:: - - >>> payload = {'key1': ['value1', 'value2']} - >>> r = requests.post('http://httpbin.org/post', data=payload) - >>> print(r.text) + >>> payload_tuples = [('key1', 'value1'), ('key1', 'value2')] + >>> r1 = requests.post('http://httpbin.org/post', data=payload_tuples) + >>> payload_dict = {'key1': ['value1', 'value2']} + >>> r2 = requests.post('http://httpbin.org/post', data=payload_dict) + >>> print(r1.text) { ... "form": { @@ -277,6 +264,8 @@ a dictionary and list the element values as a list:: }, ... } + >>> r1.text == r2.text + True There are times that you may want to send data that is not form-encoded. If you pass in a ``string`` instead of a ``dict``, that data will be posted directly.