我正在尝试使用 requests.post 向 WheniWork API 发送一个请求数组(列表) ,但总是出现两个错误之一。当我以列表的形式发送列表时,我会得到一个解包错误,当我以字符串的形式发送列表时,我会得到一个要求我提交数组的错误。我认为这与请求处理列表的方式有关。下面是一些例子:
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}