我正在尝试在multipart / form-data content-type的POST Request中将excel文件发送到服务器。我收到一个错误:
要解压的值太多
可能是什么原因?以下是我正在尝试的请求:
#Data = get_data('C:\foo.xls')
#print Data
Data = open('C:\foo.xls', 'rb')
print Data
headers = {
'access-control-allow-origin': '*',
'accept': 'application/json',
'content-type': 'multipart/form-data',
'authorization': 'Basic xxxxxxxxx'
}
R = requests.post('http://testserver:8080/v1/readyapi/executions/'+executionId+'/files', headers=headers, params=params, files=Data)
print R.content
这是错误:
Traceback (most recent call last):
(body, content_type) = self._encode_files(files, data)
File "C:\Python27\lib\site-packages\requests\models.py", line 132, in _encode_files
for (k, v) in files:
ValueError: too many values to unpack
我自己也搞不清楚。尝试了几件事,没有用。有人可以建议吗?
答案 0 :(得分:0)
尝试以下代码
Data = open('C:\foo.xls', 'rb')
headers = {
'access-control-allow-origin': '*',
'accept': 'application/json',
'content-type': 'multipart/form-data',
'authorization': 'Basic xxxxxxxxx'
}
files = {"file_name": Data}
url = 'http://testserver:8080/v1/readyapi/executions/'+executionId+'/files'
R = requests.post(url, headers=headers, params=params, files=files)
print R.content
您必须将files参数作为字典传递 要么 你也可以尝试下面的
files = {'file': ('foo.xls', open('foo.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}
参考:http://docs.python-requests.org/en/master/user/quickstart/#post-a-multipart-encoded-file