Open dschulten opened 5 years ago
I have prepared a branch for a possible PR: https://github.com/dschulten/python-flask-template/tree/multipart-form-data-support, with updated README.
Demo: https://github.com/dschulten/python-flask-template/tree/PyPDF2-merge, see https://github.com/dschulten/python-flask-template/blob/PyPDF2-merge/template/python3-http/function/handler.py
Documentation with sample request: https://github.com/dschulten/python-flask-template/tree/multipart-form-data-support#working-with-multipart-requests
@dschulten Hi - this is great! I haven't look at it yet, but it is something that I needed for one of my functions. . I've done some hackery, but a clean and reusable solution is better.
is there a reason why this was not merged? I feel like the edit is pretty straightforward - just adding some properties to the Event class. Came here to send a PR for it, but seeing that the issue is two years old makes me think that it will be ignored.
The python3-http templates use an
Event
abstraction which represents the incoming request. Currently theEvent
only holds therequest.data
as body, which is the body as byte array.Suggestion: Leverage Flask's support for multipart/form-data, application/json (and x-www-form-urlencoded) by adding the following attributes to the
Event
object:request.files
andrequest.form
- they are needed formultipart/form-data
requests. Therequest.files
attribute is anImmutableMultiDict
ofFileStorage
(which is a file-ish ducktype) where every entry is a list of files. Therequest.form
is anImmutableMultiDict
of strings which represent the textual multiparts which have no filename in their content-disposition (or the values of an x-www-form-urlencoded body). We already preserve ImmutableMultiDict in Event.query, so it seems straightforward to addfiles
andform
, toorequest.get_json()
- it only gets filled when the request is ofContent-Type: application/json
The format_body method also needs to be adjusted so that it uses Flask's support for file responses if the handler returns a file.
This gives us the ability to upload files to such functions and download the function result (would also enable us to have uploads in the OpenFaaS Dashboard). Depending on Flask's implementation this also gives us support for large files.
I am preparing a PR for the python3-http template, and I could do likewise for the IRequest in the Java Template.
Please let me know if you approve of the general idea.