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

StackOverflow https://stackoverflow.com/questions/21998009

  •  16-10-2022
  •  | 
  •  

Вопрос

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)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top