我尝试使用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}
发布于 2016-02-21 17:58:39
事实证明,我所需要做的就是添加这些头文件:
headers = {'Content-Type': 'application/json', 'Accept':'application/json'}
然后调用请求
requests.post(url,data=json.dumps(payload), headers=headers)
现在我好了!
发布于 2019-11-08 04:15:14
我有一个类似的案例,但完全不同的解决方案,我复制了一个代码片段,看起来像这样:
resp_status, resp_data = requests.post(url, headers=headers,
json=payload, verify=False)
这导致了错误:
ValueError: too many values to unpack (expected 2)
只需为一个变量赋值即可解决问题:
response = requests.post(url, headers=headers,
json=payload, verify=False)
https://stackoverflow.com/questions/31168819
复制相似问题