如何在python请求中使用相同的键发布多个值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23384230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to post multiple value with same key in python requests?
提问by Jasonyi
requests.post(url, data={'interests':'football','interests':'basketball'})
I tried this, but it is not working. How would I post football
and basketball
in the interests
field?
我试过这个,但它不起作用。我将如何发布football
和basketball
在interests
现场?
采纳答案by Martijn Pieters
Dictionary keys mustbe unique, you can't repeat them. You'd use a sequence of key-value tuples instead, and pass this to data
:
字典键必须是唯一的,不能重复。您将改用一系列键值元组,并将其传递给data
:
requests.post(url, data=[('interests', 'football'), ('interests', 'basketball')])
Alternatively, make the values of the data
dictionary lists; each value in the list is used as a separate parameter entry:
或者,使data
字典列表的值;列表中的每个值都用作单独的参数条目:
requests.post(url, data={'interests': ['football', 'basketball']})
Demo POST to http://httpbin.org:
演示 POST 到http://httpbin.org:
>>> import requests
>>> url = 'http://httpbin.org/post'
>>> r = requests.post(url, data=[('interests', 'football'), ('interests', 'basketball')])
>>> r.request.body
'interests=football&interests=basketball'
>>> r.json()['form']
{u'interests': [u'football', u'basketball']}
>>> r = requests.post(url, data={'interests': ['football', 'basketball']})
>>> r.request.body
'interests=football&interests=basketball'
>>> r.json()['form']
{u'interests': [u'football', u'basketball']}
回答by Sjoerd
It is possible to use urllib3._collections.HTTPHeaderDict
as a dictionary that has multiple values under a key:
可以urllib3._collections.HTTPHeaderDict
用作在一个键下具有多个值的字典:
from urllib3._collections import HTTPHeaderDict
data = HTTPHeaderDict()
data.add('interests', 'football')
data.add('interests', 'basketball')
requests.post(url, data=data)
回答by Mooncrater
Quoting from the docsdirectly:
直接从文档中引用:
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_tuples = [('key1', 'value1'), ('key1', 'value2')] >>> r1 = requests.post('https://httpbin.org/post', data=payload_tuples) >>> payload_dict = {'key1': ['value1', 'value2']} >>> r2 = requests.post('https://httpbin.org/post', data=payload_dict) >>> print(r1.text) { ... "form": { "key1": [ "value1", "value2" ] }, ... } >>> r1.text == r2.text True
data 参数也可以为每个键有多个值。这可以通过将数据作为元组列表或以列表作为值的字典来完成。当表单有多个使用相同键的元素时,这特别有用:
>>> payload_tuples = [('key1', 'value1'), ('key1', 'value2')] >>> r1 = requests.post('https://httpbin.org/post', data=payload_tuples) >>> payload_dict = {'key1': ['value1', 'value2']} >>> r2 = requests.post('https://httpbin.org/post', data=payload_dict) >>> print(r1.text) { ... "form": { "key1": [ "value1", "value2" ] }, ... } >>> r1.text == r2.text True