Get the data received in a Flask request
The docs describe the attributes available on the
request object (
from flask import request) during a request. In most common cases
request.data will be empty because it's used as a fallback:
request.dataContains the incoming request data as string in case it came with a mimetype Flask does not handle.
request.args: the key/value pairs in the URL query string
request.files: the files in the body, which Flask keeps separate from
form. HTML forms must use
enctype=multipart/form-dataor files will not be uploaded.
argsif keys overlap
request.json: parsed JSON data. The request must have the
application/jsoncontent type, or use
request.get_json(force=True)to ignore the content type.
All of these are
MultiDict instances (except for
json). You can access values using:
request.form['name']: use indexing if you know the key exists
getif the key might not exist
getlistif the key is sent multiple times and you want a list of values.
getonly returns the first value.
For URL query parameters, use
search = request.args.get("search")page = request.args.get("page")
For posted form input, use
email = request.form.get('email')password = request.form.get('password')
For JSON posted with content type
data = request.get_json()