【问题标题】:Forwarding multipart/form-data to different service (python, bottle, requests)将多部分/表单数据转发到不同的服务(python、bottle、请求)
【发布时间】:2014-02-25 14:45:56
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: python bottle wtforms


    【解决方案1】:

    想出了如何让它工作,实际上是我做错了,下面的代码可以工作

    @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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-01
      • 2018-03-05
      相关资源
      最近更新 更多