python - 将curl请求转换为python请求

标签 python curl python-requests

我正在尝试将 cURL 命令转换为 python 请求,但遇到了困难。也许有人会有想法。

我正在使用这个site供引用,我有这样一个 cURL 命令:

curl -F "file=@myfile.json" -H "Authorization: ApiKey xxxxxxxxxxx" -F 'scan_type=Arachni Scan' -F 'tags=test' -F 'active=true' -F 'scan_date=2018-11-15' -F 'engagement=/api/v1/engagements/1/'  -F 'eid=1' https://example.com:443/api/v1/importscan/

转换为 python 后,我的脚本摘录如下所示:

files = {
    'file': ('myfile.json', open('myfile.json', 'rb')),
    'scan_type': 'Arachni Scan',
    'tags': 'test',
    'active': 'true',
    'scan_date': '2018-11-15',
    'engagement': '/api/v1/engagements/1/',
    'eid': '1',
}

response = requests.post('https://example.com:443/api/v1/importscan/', headers=headers, files=files)
print response.text

不幸的是,cURL命令成功了,但是python请求返回了

{"error": "Request is not valid JSON."}

我在这里缺少什么?谢谢!

最佳答案

import json

response = requests.post('https://example.com:443/api/v1/importscan/', headers=headers, files=json.dumps(files))

使用 json.dumps 就可以了。

关于python - 将curl请求转换为python请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53323202/

相关文章:

python - 无法从网站下载 CSV 文件

python - Python请求中 "data"和 "params"之间的区别?

python - git 如何获取与文件关联的提交?

python - 关于正则表达式和 XML

php - CURLOPT_PUT 与 CURLOPT_POSTFIELDS

powershell - curl http ://url/script. ps1 | PowerShell 可能吗?

python - 以秒为单位的时间差(作为 float )

python - tkinter filedialog.askopenfilename() 窗口在 python 3 中不会关闭

php - 在vBulletin中使用curl登录网站

python - BeautifulSoup - 爬行维基页面