将多部分/表单数据转发到不同的服务(Python,瓶子,请求) [英] Forwarding multipart/form-data to different service (python, bottle, requests)

查看:59
本文介绍了将多部分/表单数据转发到不同的服务(Python,瓶子,请求)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个中间层api,它从客户端接收请求(可能带有附件的表单提交请求),并验证两件事(使用WTForms进行表单验证),然后将表单发布请求转发到实际上对此执行操作的另一个服务.

I have middle-layer api which receives request (form submit request with possibly with attachment) from client and verify couple of things (form validation using WTForms) and then forward form post request to another service which actually performs actions on that.

我面临的问题无法按原样转发请求数据和附加的文件,下面是代码示例.

Problem I am facing is not able to forward request data and files attached as it is, below is code example.

@post('/')
def index():
    post_data = request.POST.dict

    requests.post("http://127.0.0.1:8090/", data=post_data, files=request.files)

推荐答案

弄清楚了如何使其工作,实际上我做错了,下面的代码可以工作

Figured out how to make it work, actually something that I was doing wrong, code below will work

@post('/')
def index():
    form_data = request.form.dict
    file_data = request.files.get("myfile", "") 
    files = {file_data.name: (file_data.filename, file_data.file, file_data.type)}
    requests.post("http://127.0.0.1:8090/", data=form_data, files=files)

这篇关于将多部分/表单数据转发到不同的服务(Python,瓶子,请求)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆