Skip to content
Myr

Merge pull request #4701 from akaihola/4700-multivalued-list-form-encoded

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Documentation and tests for form-encoded multivalued elements as lists

authored by Nate Prewitt · committed by GitHub · parents 4d51e6d, 9c92f75 · Browse files · 3 files+18 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+18 added−5 removed

  • tests:+6 added−0 removed
  • other:+12 added−5 removed

3 files+18 added−5 removed

modified:docs/user/quickstart.rst+11 added-5 removed
242 unmodified lines
243 },244 ...245 }246247You can also pass a list of tuples to the ``data`` argument. This is particularly248useful when the form has multiple elements that use the same key::247The ``data`` argument can also have multiple values for each key. This can be248done by making ``data`` either a list of tuples or a dictionary with lists249as values. This is particularly useful when the form has multiple elements that250use the same key::251250 >>> payload = (('key1', 'value1'), ('key1', 'value2'))251 >>> r = requests.post('http://httpbin.org/post', data=payload)252 >>> print(r.text)252 >>> payload_tuples = [('key1', 'value1'), ('key1', 'value2')]253 >>> r1 = requests.post('http://httpbin.org/post', data=payload_tuples)254 >>> payload_dict = {'key1': ['value1', 'value2']}255 >>> r2 = requests.post('http://httpbin.org/post', data=payload_dict)256 >>> print(r1.text)257 {258 ...259 "form": {260 "key1": [
2 unmodified lines
263 ]264 },265 ...266 }267 >>> r1.text == r2.text268 True269270There are times that you may want to send data that is not form-encoded. If271you pass in a ``string`` instead of a ``dict``, that data will be posted directly.272
292 unmodified lines
modified:tests/test_requests.py+6 added
866 unmodified lines
867 r = requests.get(httpbin('get'), params=dict(test=['foo', 'baz']))868 assert r.status_code == 200869 assert r.url == httpbin('get?test=foo&test=baz')870871 def test_form_encoded_post_query_multivalued_element(self, httpbin):872 r = requests.Request(method='POST', url=httpbin('post'),873 data=dict(test=['foo', 'baz']))874 prep = r.prepare()875 assert prep.body == 'test=foo&test=baz'876877 def test_different_encodings_dont_break_post(self, httpbin):878 r = requests.post(httpbin('post'),879 data={'stuff': json.dumps({'a': 123})},880 params={'blah': 'asdf1234'},
1596 unmodified lines
modified:AUTHORS.rst+1 added
186 unmodified lines
187- Nehal J Wani (`@nehaljwani <https://github.com/nehaljwani>`_)188- Demetrios Bairaktaris (`@DemetriosBairaktaris <https://github.com/demetriosbairaktaris>`_)189- Darren Dormer (`@ddormer <https://github.com/ddormer>`_)190- Rajiv Mayani (`@mayani <https://github.com/mayani>`_)191- Antti Kaihola (`@akaihola <https://github.com/akaihola>`_)