In web engineering, incoming client data arrives in various formats depending on the client (browser HTML form, mobile JSON client, multipart file upload, or URL query parameters). Flask exposes a context-local global object called request that encapsulates the entire incoming HTTP request.
1. Importing and Context Mechanics
1from flask import Flask, request
Even though request appears to be a global Python variable, it is actually a thread-safe context proxy. When a request arrives, Flask binds the current thread's WSGI environment to request. Two concurrent requests processed on different threads will each access their own distinct request data without race conditions!
3. Best Practice: Handling Multi-Valued Keys with getlist()
HTML forms with checkboxes (<input type="checkbox" name="skills" value="Python">) send multiple values for the same key. Using .get() will only return the first value! Use getlist() instead:
1@app.route("/profile/skills", methods=["POST"])
2defupdate_skills():
3# Returns a Python list: ['Python', 'Flask', 'SQLAlchemy']
4 skills = request.form.getlist("skills")
5return{"selected_skills": skills}
Interactive Quiz• Knowledge Check
Practice Quiz
Question 1
What type of data structure does request.args return in Flask?
Question 2
Why is request.args.get('page', default=1, type=int) preferred over int(request.args['page'])?
Question 3
Which method should be used to retrieve all selected values from an HTML checkbox group sharing the same name?
Question 4
What does passing silent=True to request.get_json(silent=True) accomplish?
Question 5
How is the client's IP address accessed on the Flask request object?
Next Lesson
Response Objects, make_response & Cookies
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.