我正在尝试将以下curl请求转换为python@3.8密码curl将excel文件数据发送到服务器
curl请求被正确解析,但是我的python代码不起作用,我从服务器收到了一个错误的请求
curl -X POST "http://localhost:8080/rest/2.0/import/ejob" -H "accept: application/json" -H "Authorization: Basic xyz" -H "Content-Type: multipart/form-data" -F "deleteFile=" -F "simulation=" -F "fileName=import_file" -F "sheetName=" -F "headerRow=" -F "sendNotification=" -F "sheetIndex=" -F "fileId=" -F 'template= [ { "resourceType": "xyz", "type": { "name": "xyz" }, "identifier": { "name": "${1}", "domain": { "name": "xyz", "community": { "name": "test" } } } } ]' -F "batchSize=10000" -F "file=@filename.xlsx;type=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Python代码:
url = "http://localhost:8080/rest/2.0/import/ejob"
payload={'deleteFile': '',
'simulation': '',
'fileName': 'xyz',
'sheetName': '',
'headerRow': '',
'sendNotification': '',
'sheetIndex': '',
'fileId': '',
'template': ' [ { "resourceType": "Asset", "type": { "name": "xyz" }, "identifier": { "name": "${1}", "domain": { "name": "xyz", "community": { "name": "file" } } } } ]',
'batchSize': '10000',
'file': '@filename.xlsx;type=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}
files = {'file': ('import_file', open('filename.xlsx', 'rb'), 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', {'Expires': '0'})}
headers = {
'accept': 'application/json',
'Authorization': 'Basic xyz',
'Content-Type': 'multipart/form-data',
}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
print(response.text)
对于从python发送的请求,在Wireshark中看不到边界。下面的屏幕截图是针对CURL的
问题在于在标头中传递“内容类型”:“多部分/表单数据”。我从有效负载中删除了该文件,并修改了头文件以修复该问题
试试这个:
在不知道将请求发送到哪个服务器的情况下很难确定,但我猜您的问题是在
data
和files
参数中复制了file
表单字段当您在传递给
data
和files
参数的dict中使用相同的键时,请求将对请求使用多部分编码,并使用来自这两个dict的数据例如,当我在系统上尝试以下代码时:
它转换为以下HTTP请求:
在您的情况下,可能是服务器在HTTP请求中看到了第一个
file
表单字段,发现它没有filename
属性,并决定拒绝格式错误的请求相反,请尝试从
payload
字典中删除file
键,以便在生成的HTTP请求中只有一个file
表单字段顺便说一句,部分问题是您在Python
payload
dict的file
键中使用了@filename.xlsx
,但是@
是一种特殊的curl语法,它在请求中包含指定文件的内容从curl man page开始:
在请求库中,
@
没有这种效果;相反,它只是作为普通字符插入到请求中相关问题 更多 >
编程相关推荐