我尝试使用requests.post向WheniWork接口发送一个请求数组(列表),但一直收到两个错误中的一个。当我将列表作为列表发送时,我得到一个解包错误,当我将它作为字符串发送时,我得到一个错误,要求我提交一个数组。我认为这与请求处理列表的方式有关。下面是一些示例:
url='https://api.wheniwork.com/2/batch'
headers={"W-Token": "Ilovemyboss"}
data=[{'url': '/rest/shifts', 'params': {'user_id': 0,'other_stuff':'value'}, 'method':'post',{'url': '/rest/shifts', 'params': {'user_id': 1,'other_stuff':'value'}, 'method':'post'}]
r = requests.post(url, headers=headers,data=data)
print r.text
# ValueError: too many values to unpack
简单地将数据的值括在引号中:
url='https://api.wheniwork.com/2/batch'
headers={"W-Token": "Ilovemyboss"}
data="[]" #removed the data here to emphasize that the only change is the quotes
r = requests.post(url, headers=headers,data=data)
print r.text
#{"error":"Please include an array of requests to make.","code":5000}
https://stackoverflow.com/questions/31168819
复制相似问题