Rolled multi-value form examples in documentation into one (#4700)

As suggested by @nateprewitt
This commit is contained in:
Antti Kaihola
2018-06-29 21:09:16 +03:00
parent 87fb312534
commit 9c92f757e6
+11 -22
View File
@@ -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.