Get the data received in a Flask request
This video is brought to you by Memorabit, an app that sends technical terms, commands and definitions as reminders at unexpected moments, so they stay in memory. Built on cognitive science principles. Start free on web, iOS and Android:
iOS: https://apps.apple.com/app/id6782301691
Android: https://play.google.com/store/apps/detai...
--------------------------------------------------
Read the revolutionary AI books written by Turbina Editore, the first publishing house entirely run by artificial intelligence. This groundbreaking publisher has transformed the literary world, producing the most sought-after AI-generated books. Start reading today at:
https://turbinaeditore.com/en/
--------------------------------------------------
--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT
Music by Eric Matyas
https://www.soundimage.org
Track title: Over a Mysterious Island Looping
--
Chapters
00:00 Get The Data Received In A Flask Request
00:34 Answer 1 Score 279
00:52 Accepted Answer Score 2248
02:25 Answer 3 Score 320
02:50 Answer 4 Score 92
03:15 Thank you
--
Full question
https://stackoverflow.com/questions/1043...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #flask #werkzeug
#avk47
ACCEPTED ANSWER
Score 2248
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 stringrequest.form: the key/value pairs in the body, from a HTML post form, or JavaScript request that isn't JSON encodedrequest.files: the files in the body, which Flask keeps separate fromform. HTML forms must useenctype=multipart/form-dataor files will not be uploaded.request.values: combinedargsandform, preferringargsif keys overlaprequest.json: parsed JSON data. The request must have theapplication/jsoncontent type, or userequest.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 existsrequest.form.get('name'): usegetif the key might not existrequest.form.getlist('name'): usegetlistif the key is sent multiple times and you want a list of values.getonly returns the first value.
ANSWER 2
Score 320
For URL query parameters, use request.args.
search = request.args.get("search")
page = request.args.get("page")
For posted form input, use request.form.
email = request.form.get('email')
password = request.form.get('password')
For JSON posted with content type application/json, use request.get_json().
data = request.get_json()
ANSWER 3
Score 279
To get the raw data, use request.data. This only works if it couldn't be parsed as form data, otherwise it will be empty and request.form will have the parsed data.
from flask import request
request.data
ANSWER 4
Score 92
To get the raw post body regardless of the content type, use request.get_data(). If you use request.data, it calls request.get_data(parse_form_data=True), which will populate the request.form MultiDict and leave data empty.