Created
October 10, 2021 14:58
-
-
Save sphaugh/101c980d632ea2a6ccbbe03ac8526697 to your computer and use it in GitHub Desktop.
Form submission with Werkzeug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from base64 import b64decode | |
from functools import wraps | |
from io import BytesIO, StringIO | |
from werkzeug.datastructures import Headers | |
from werkzeug.formparser import parse_form_data | |
def use_form(func): | |
@wraps(func) | |
def func_with_form_data(event, *args, **kwargs): | |
body = b64decode(event["body"]) if event["isBase64Encoded"] else event["body"] | |
event["headers"] = Headers(event["headers"]) | |
environ = { | |
"wsgi.input": BytesIO(body) if isinstance(body, bytes) else StringIO(body), | |
"CONTENT_LENGTH": len(body), | |
"CONTENT_TYPE": event["headers"]["Content-Type"], | |
"REQUEST_METHOD": "POST", | |
} | |
_, event["form"], event["files"] = parse_form_data(environ) | |
return func(event, *args, **kwargs) | |
return func_with_form_data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment