Forwarding multipart/form-data to different service (python, bottle, requests)

你离开我真会死。 提交于 2020-01-05 21:20:08

问题


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)

回答1:


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)


来源:https://stackoverflow.com/questions/21998009/forwarding-multipart-form-data-to-different-service-python-bottle-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!