1

I'm using flask to store audio files. When sending the post request to store the file, I'd also like to send additional information about the file, specifically a boolean value.

Currently I'm accessing the file with flask by doing:

file = request.files['file']

I'm sending the file to the flask server in swift by doing:

request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
request.HTTPBody = createBodyWithParameters(param, filePathKey: "file", path: params["url"] as? NSURL, boundary: boundary)

Is there a way I can obtain the boolean value with the file upload?

Brosef
  • 2,945
  • 6
  • 34
  • 69

1 Answers1

0

A form when submitted can have more than one value specified by the name attribute on the html element inside that form.

<form>
   <input type="text" value="true" name="isFileUploaded">
   <input type="file" name="myFile">
</form>

To get the parameters sent to the server, you can always use

POST

request.form.get('<your_name_used_in_the_form'>) #in this case its isFileUploaded

GET

request.args.get('<your_name_used_in_the_form>')

Read this answer for more...

Community
  • 1
  • 1
cafebabe1991
  • 4,928
  • 2
  • 34
  • 42
  • Thanks for the explanation. I see that I can easily access the file by doing `file = request.files['file']`. Can I access the contents within `param` just as easily? In the past, I would handle a regular post request by doing `data = MultiDict(mapping=request.json)`, but that won't work in this case. – Brosef Jun 22 '16 at 03:13
  • Just try it out via request.form.get('param_name') , it should work as its a normal that is being submitted to you. In ideal scenario this should work. – cafebabe1991 Jun 22 '16 at 03:17
  • i'm not using a form so I don't think `request.form.get('param_name')` will work. Thats why I've been using `data = MultiDict(mapping=request.json)`. When I type `request`, `request.form` isn't an option. – Brosef Jun 22 '16 at 03:34