Table of contents
1.
Introduction
2.
Attributes of a Flask Request Object
3.
Data Retrieval Methods
4.
Using Query Arguments
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

Flask Request Object

Author Parth Jain
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

When we access a website, we receive a lot of information from the website's server into our browser. This information can be in images, texts, videos, and objects.
However, our browser only displays the intended result of all the information as intended by the programmer. 
All we do is request our browser for a particular website by typing its name on the URL tab but, what exactly happens when we do that? 
Our browser requests a response from the server of the website we tried to access and, in response, receive an object. This object contains all the data that our browser then understands and displays accordingly.
Then, as we press different buttons or images of the website, the behavior of the website keeps changing as the browser keeps requesting the data from the server.
This Client-Server relationship is possible with the help of Flask Request Object.
The Flask Request Object is used to perform both sending and receiving operations from a user's browser to the server and process the request data from the server. It should be imported from the flask module.

Attributes of a Flask Request Object

As discussed above, the data from a client’s webpage is sent to the server as a global request object, and to process this received object, it should be imported from the Flask Module.
The request objects have specific attributes attached to them:

SN.

Attributes

Description

1.

Form

Houses various Key-Value pairs of form parameters

2.

Args

The URL contents that are located after the (?)

3.

Cookies

These are temporary files used to track user sessions as they hold the names and values of cookies.

4.

Files

Holds every information about the uploaded file

5.

Method

Displays the ongoing request method (POST, GET, etc.)

6.

Values

These are combined dictionaries that contain data of both forms and arguments.

7.

Data

Contains the incoming request data in strings

8.

Headers

Stores the incoming request as a dictionary-like object

9.

Module

The current module's name if the request was dispatched to an actual module. 

10.

Environ

The underlying WSGI environment

 

Data Retrieval Methods

There are three ways by which data can be retrieved from the request object.

  • Using Query Arguments
  • Using Form Data
  • Using JSON Data

This blog focuses on the Query Arguments method.

Using Query Arguments

One of the easiest ways to pass data to the web is using query arguments. As discussed in the above table, query arguments are values located after the (?) in the URL of a website. 

somewebsite.com?arg1=val1&arg2=val2&arg3=val3
The query string is situated after the question mark (?)

somewebsite.com?arg1=val1&arg2=val2&arg3=val3
The query string contains key-value pairs that are separated from each other by the ampersand symbol (&)

somewebsite.com?arg1=val1&arg2=val2&arg3=val3
The key-value pairs are followed by an equals symbol. However, they can be comprehended like this

arg1: val1

arg2: val2

arg3: val3
Query Strings can be generated anywhere in the application and appended directly to the URL. They do not require the user to take action. They become crucial for passing data.
Let us create our Query String to understand it even more.

http://127.0.0.1:5000/query-string_example?language=Javascript&framework=React&description=test&website=CodingNinjas
The above URL contains four key-value pairs that can be imported using the Flask Request Module.

app.py

# import main Flask class and the request object
from flask import Flask, request


# create the Flask app
app = Flask(__name__)


 @app.route('/query-string_example')
def query_example():
   
    language = request.args.get('language') 
# returns None if no key is entered

    
    framework = request.args['framework']
# returns a 400,if key does not exist


description= request.args.get('description')
# returns None,if key in not entered


    website = request.args.get('website')
# returns None,if key in not entered


    return '''
              <p>The language value is: {}</p>
              <p>The framework value is: {}</p>
<p>Description: {}</p>
              <p>The website value is: {}</p>'''.format(language, framework,description, website)
You can also try this code with Online Python Compiler
Run Code

Output:

The language value is: Javascript
The framework value is: React
Description:test
The website value is: CodingNinjas

Recommended Topic, PHP For Loop

FAQs

  1. What is a request object in Flask?
    The information from a server is received in the form of an object which can be accessed using the request object as it houses all the information such as text data, images, videos, and key-value pairs.
     
  2. What all attributes are required in Flask Request Object?
    Essential attributes of the request object are
  • Form − Houses various Key-Value pairs of form parameters
  • args − The URL contents located after the (?)
  • Cookies − These are temporary files used to track user sessions as they hold the names and values of cookies.
  • Files − data about uploaded files.
  • Method − current request method.

Key Takeaways

In this article, we learned about Flask Request Objects and their importance when sending and receiving data in a client-server relationship. However, this isn't enough, as there is always much more to explore and learn about this vast field of Web Development. To know more about Flask and its intricacies, check out the articles on Flask or enroll in our highly curated Web Development course. 

Read more: Attributes in DBMS

Live masterclass