Redirect Python flask:将POST转发到外部API
我正在使用python flask和FlaskRestPlus、python 3.7.0为Camunda rest API构建一个微服务。Camunda在docker容器中运行,可通过localhost端口8080获得。对我的微服务的所有GET请求都通过重定向转发到Camunda API,该重定向工作正常 POST请求(按照官方教程中的建议,通过邮递员进行测试)未使用Redirect Python flask:将POST转发到外部API,redirect,post,flask,microservices,python-3.7,Redirect,Post,Flask,Microservices,Python 3.7,我正在使用python flask和FlaskRestPlus、python 3.7.0为Camunda rest API构建一个微服务。Camunda在docker容器中运行,可通过localhost端口8080获得。对我的微服务的所有GET请求都通过重定向转发到Camunda API,该重定向工作正常 POST请求(按照官方教程中的建议,通过邮递员进行测试)未使用 重定向(camunda\u api\u url) 或 request.post(camunda\u api\u url) 通过邮
重定向(camunda\u api\u url)
或
request.post(camunda\u api\u url)
通过邮递员的POST请求是使用
Header: Content-Type: multipart/form-data
Body: upload File Object (somefile.bpmn)
当我直接向Camunda REST-API发送帖子时,一切正常,但当我尝试通过我的微服务重定向帖子时,我得到状态代码200,但文件没有上传
在我的端点调试时,我可以看到正在接收文件:
print(request.files['upload'])
<FileStorage: 'somefile.bpmn' ('application/octet-stream')>
通过修改request.post
data=request.files
data=request.files['upload']
or omiting data completely
始终导致文件未上载
正在尝试通过
重定向(主机前缀+'deployment/create',code=307)
也会导致文件无法上载
如何将此post请求正确重定向到Camunda API?
这不是关于Camunda API的问题,而是关于如何将POST请求正确重定向到外部端点的问题
备注:我创建了api和端点,如下所示:
def post(self):
print(request.files['upload'])
test = requests.post(host_prefix + 'deployment/create', files=request.files)
print(test.status_code)
app = Flask(__name__)
api = Api(app, version='0.1', title='BPMN-API', description='A BPMN-API for Camunda, implemented in python')
...
api.add_resource(CreateDeployment, api_prefix + 'deployment/create', methods=['POST'])
好的,我使用以下方法解决了这个问题:
requests.post(camunda\u api\u url,files={file\u name:request.files['upload'].read()})
其中,camunda_api_url是camunda REST引擎的端点,file_name是正在上载的文件的名称,通过添加
获取方法,只需使用:
def get(self):
camunda_api_url = "http://localhost:8080/engine-rest/deployment/create"
return redirect(camunda_api_url)
如果没有get方法,post将无法工作
->将主题设置为已解决。:) 使用请求库非常简单。该示例显示了使用同一密钥以及其他表单数据的多个文件上载。图像来自一个名为“
images
”的表单
该示例从带有键“images”的表单获取文件列表,并将该文件转发到另一个URL或API
images = request.files.getlist('images')
files = []
for image in images:
files.append(("images", (image.filename, image.read(), image.content_type)))
r = requests.post(url=api_urls.insert_face_data_url, data={"apikey": apikey, "faceid": faceid},
files=files)