简体   繁体   English

将远程文件传递给python中的http post请求

[英]Passing a remote file to a http post request in python

I have a rest api that offers an upload file post functionality.我有一个提供上传文件发布功能的休息 api。 To test this, I simply did this on my python test code:为了测试这个,我只是在我的 python 测试代码上做了这个:

fo = open('file.zip', 'rb')
rb_file = {'confile': ('file.zip', fo, 'multipart/form-data')}
resp = requests.post(url,files=rb_file)
fo.close()

This request returns a uuid response {ce9f2d23-8ecd-4c60-9d31-aef0be103d44} that is needed for initiating another post run for a scan.此请求返回一个 uuid 响应{ce9f2d23-8ecd-4c60-9d31-aef0be103d44} ,这是启动另一个扫描后运行所需的。

From Swagger, manually passing this uuid to the scan post request generates the following curl:从 Swagger 中,手动将此 uuid 传递到扫描发布请求会生成以下 curl:

curl -X POST "http://....../scan" -H "Content-Type: multipart/form-data" -F "FilID=ce9f2d23-8ecd-4c60-9d31-aef0be103d44"

My question is how to convert this curl to a python request code noting that I no longer have the file on my local machine.我的问题是如何将此 curl 转换为 python 请求代码,并指出我的本地机器上不再有该文件。 The server seems to be expecting a -F rather than a param.服务器似乎期待 -F 而不是参数。 How do I pass a file that doesn't exist on my local machine to http request in this case?在这种情况下,如何将本地计算机上不存在的文件传递给 http 请求? All I have is the filID.我只有 filID。 I tried running as param but that doesn't find the resource.我尝试作为 param 运行,但没有找到资源。

try this !试试这个 !

import requests

headers = {
    'Content-Type': 'multipart/form-data',
}

files = {
    'FilID': (None, 'ce9f2d23-8ecd-4c60-9d31-aef0be103d44'),
}

response = requests.post('http://....../scan', headers=headers, files=files)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM