Flask applications receive data through HTTP requests. Depending on how the client sends the data, it can be accessed as query parameters, HTML form data, or JSON payloads. Flask provides the request object to retrieve and process this incoming request data.
Flask request Object
The request object represents the incoming HTTP request sent by a client. It provides access to request data such as query parameters, form fields, JSON payloads, headers, cookies, uploaded files, HTTP methods and other request metadata. Some of the properties of the request object are:
| Attribute | Purpose |
|---|---|
| request.args | Query parameters |
| request.form | Form data |
| request.get_json() | JSON request body |
| request.files | Uploaded files |
| request.headers | HTTP headers |
| request.cookies | Cookies |
| request.method | HTTP method |
Process Incoming Request Data in Flask
To perform Process Incoming Request Data in a Flask Application we will use some request properties that received the data in an efficient way, for that we will create the routes and call the requested property.
- Accesses the Query String
- Accesses the Form Data.
- Returned the JSON data.
Accesses the Query String Parameter
Query parameters are key-value pairs appended to the end of a URL after the ? symbol. They are commonly used with GET requests to send small amounts of data, such as search terms, filters, or user preferences.
Example URL:
http://127.0.0.1:5000/query_example?language=Python
Step 1: Import the Flask class and the request object, then create a Flask application instance.
from flask import Flask, request
app = Flask(__name__)
Step 2: Create a route that reads the value of the language query parameter using request.args.get().
from flask import Flask, request
app = Flask(__name__)
@app.route("/query_example")
def query_example():
language = request.args.get("language")
return f"<h1>The language is: {language}</h1>"
if __name__ == "__main__":
app.run(debug=True)
Output:
The language is: Python
Create the key 'language' and value as 'python'

Explanation:
- @app.route() maps the URL to the view function.
- request.args contains all query parameters sent with the request.
- get("language") retrieves the value associated with the language parameter.
- The retrieved value is displayed in the response.
Step 3: Multiple query parameters can be accessed by retrieving each parameter individually.
from flask import Flask, request
app = Flask(__name__)
@app.route("/query_example")
def query_example():
language = request.args.get("language")
framework = request.args.get("framework")
website = request.args.get("website")
return f"""
<h1>Language: {language}</h1>
<h1>Framework: {framework}</h1>
<h1>Website: {website}</h1>
"""
if __name__ == "__main__":
app.run(debug=True)
Output:
Language: Python
Framework: Flask
Website: flask.palletsprojects.com
Create a key 'framework' & 'website' and their value as 'Flask' & 'flask.org'

Explanation:
- Each query parameter is retrieved using request.args.get().
- Multiple parameters are separated using the & character in the URL.
- The retrieved values are displayed in the HTML response.
Accesses the Form Data
HTML forms typically send data to a Flask application using the POST method. Flask provides the request.form object to retrieve values submitted through form fields.
Step 1: Create a route that displays an HTML form for collecting user input.
from flask import Flask, request
app = Flask(__name__)
@app.route("/form_example")
def form_example():
return """
<form method="POST" action="">
<label>Language:</label>
<input type="text" name="language"><br><br>
<label>Framework:</label>
<input type="text" name="framework"><br><br>
<input type="submit" value="Submit">
</form>
"""
if __name__ == "__main__":
app.run(debug=True)
Visit,
http://127.0.0.1:5000/form_example
Output:

Step 2: Modify the route to accept both GET and POST requests. When the form is submitted, retrieve the entered values using request.form.
from flask import Flask, request
app = Flask(__name__)
@app.route("/form_example", methods=["GET", "POST"])
def form_example():
if request.method == "POST":
language = request.form.get("language")
framework = request.form.get("framework")
return f"""
<h1>Language: {language}</h1>
<h1>Framework: {framework}</h1>
"""
return """
<form method="POST" action="">
<label>Language:</label>
<input type="text" name="language"><br><br>
<label>Framework:</label>
<input type="text" name="framework"><br><br>
<input type="submit" value="Submit">
</form>
"""
if __name__ == "__main__":
app.run(debug=True)
Output:

Explanation:
- methods=["GET", "POST"] allows the route to handle both GET and POST requests.
- request.method determines the type of HTTP request received.
- request.form contains the data submitted through the HTML form.
- request.form.get() retrieves the value associated with each form field.
- The submitted values are displayed in the response.
Note: request.form.get() returns None if the field is missing, whereas request.form["field_name"] raises a KeyError. Using get() is recommended when form fields are optional.
Accesses the JSON Data
JSON is the format used for exchanging data between clients and REST APIs. Flask provides the request.get_json() method to parse the JSON request body and return it as a Python dictionary.
Step 1: Create a route that accepts POST requests.
from flask import Flask, request
app = Flask(__name__)
@app.route("/json_example", methods=["POST"])
def json_example():
return "Send a JSON request to this endpoint."
if __name__ == "__main__":
app.run(debug=True)
Visit,
http://127.0.0.1:5000/json_example
Output:

Step 2: The endpoint can be tested using tools such as Postman, Insomnia, curl, or any REST API client.
Select:
- HTTP Method: POST
- Body - Raw - JSON

Step 3: Now, sync some data over to Flask through Postman, change the data to row and change the type to JSON application after then we will create a JSON object in postman.
{
"language" : "Python",
"framework" : "Flask",
"website" : "scotch",
"version_info" : {
"python" : 3.7,
"flask" : 1.0
},
"example" : [ "query", "form", "json"],
"boolean_test" : true
}

Step 4: Now here we will implement the JSON code and add all the JSON Object code in the Python file.
from flask import Flask, request
app = Flask(__name__)
@app.route("/json_example", methods=["POST"])
def json_example():
req_data = request.get_json()
language = req_data["language"]
framework = req_data["framework"]
python_version = req_data["version_info"]["python"]
example = req_data["example"][0]
boolean_test = req_data["boolean_test"]
return f"""
<h1>Language: {language}</h1>
<h1>Framework: {framework}</h1>
<h1>Python Version: {python_version}</h1>
<h1>Example: {example}</h1>
<h1>Boolean Value: {boolean_test}</h1>
"""
if __name__ == "__main__":
app.run(debug=True)
Output:
