14
requests.post(url, data={'interests':'football','interests':'basketball'})
但是,是不是工作,如何發佈football
和basketball
在interests
字段?如何在python請求中使用相同的鍵發佈多個值?
requests.post(url, data={'interests':'football','interests':'basketball'})
但是,是不是工作,如何發佈football
和basketball
在interests
字段?如何在python請求中使用相同的鍵發佈多個值?
字典鍵必須是唯一的,你不能重複它們。你會使用鍵值元組序列代替,並通過這data
:或者
requests.post(url, data=[('interests', 'football'), ('interests', 'basketball')])
,使data
字典的值名單;列表中的每個值被用作一個獨立的參數項:
requests.post(url, data={'interests': ['football', 'basketball']})
演示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']}
它可以使用urllib3._collections.HTTPHeaderDict
作爲一個鍵下有多個值的字典:
from urllib3._collections import HTTPHeaderDict
data = HTTPHeaderDict()
data.add('interests', 'football')
data.add('interests', 'basketball')
requests.post(url, data=data)
'requests.post(url,data = {'interest':['football','basketball']})'? –
@GuilhermeGarnier:這也可以。 –