2014-04-30 59 views

回答

28

字典鍵必須是唯一的,你不能重複它們。你會使用鍵值元組序列代替,並通過這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']} 
+3

'requests.post(url,data = {'interest':['football','basketball']})'? –

+0

@GuilhermeGarnier:這也可以。 –

3

它可以使用urllib3._collections.HTTPHeaderDict作爲一個鍵下有多個值的字典:

from urllib3._collections import HTTPHeaderDict 
data = HTTPHeaderDict() 
data.add('interests', 'football') 
data.add('interests', 'basketball') 
requests.post(url, data=data) 
相關問題