将curl请求转换为python请求

时间:2018-11-15 15:54:20

标签: 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."}

我在这里想念什么?谢谢!

1 个答案:

答案 0 :(得分:0)

import json

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

使用json.dumps可以解决问题。