python请求将文件与参数json一起上传

时间:2018-11-16 11:15:44

标签: python json python-requests

我正在尝试将json和参数一起上传,但沿途失败。也许您会有个主意。

情况是这样的。我有一个可以正常运行的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时,代码提取如下:

    filedata = {
    'file': ('myfile.json', open('myfile.json', 'rb'))
}
otherdata = {
    'scan_type': 'Arachni Scan',
    'tags': 'testing',
    'active': 'true',
    'scan_date': '2018-11-16',
    'engagement': '/api/v1/engagements/5/',
    'eid': '1',
}

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

不幸的是,服务器的响应是:

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

附加说明

我试图将所有内容放入一个这样的json数据段中,但它也不起作用。

{
'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',
}

如果有什么帮助,请使用-v标志启动cURL命令,请求标头应如下所示:

> Using Stream ID: 1 (easy handle 0x7fe63b809600)
> POST /api/v1/importscan/ HTTP/2
> Host: defectdojo.devbridge.net
> User-Agent: curl/7.54.0
> Accept: */*
> Authorization: ApiKey xxxxxxxxxxxxxxx
> Content-Length: 120555
> Content-Type: multipart/form-data; boundary=------------------------a19acdbbb9700eb3

0 个答案:

没有答案