Skip to content

Instantly share code, notes, and snippets.

@furrycatherder
Created October 10, 2021 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save furrycatherder/101c980d632ea2a6ccbbe03ac8526697 to your computer and use it in GitHub Desktop.
Save furrycatherder/101c980d632ea2a6ccbbe03ac8526697 to your computer and use it in GitHub Desktop.
Form submission with Werkzeug
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