Brosef Brosef - 1 month ago 12
Python Question

view multipart form request parameter values using flask

I'm uploading a file to my flask backend and I can't figure out how to access the parameter values in the multipart form.

I can access the uploaded file easily by doing

file = request.files['file']
but can't figure out a way to get the parameter values.

I've tried the following but haven't had any luck:

print(request.data['share'])
print(request.data['title'])
print(request.get('share'))
print(request.get('title'))

Answer

Most form inputs can be retrieved as follows:

request.form.get("fieldname")

Files can be accessed via

request.files.get("fieldname")

Where the fieldnames are the name attribute in the HTML.

Keep in mind that, just because there's a result for request.files.get("someName") doesn't mean a file was actually uploaded. You should check that the filename exists, too, in order to validate if a file was indeed uploaded.

Take for example, the following HTML

<form action="/form_endpoint" method="POST">
  <input type="text" name="data">
  <input type="submit" value="submit">
</form>

You would access the value the user input in the data field by data = request.form.get("data")

Comments