Friday, November 1, 2024
Google search engine
HomeLanguagesGet the Data Received in a Flask request

Get the Data Received in a Flask request

In this article, we will learn how we can use the request object in a flask to Get the Data Received that is passed to your routes. and How To Process Get Request Data in Flask using Python.

So, basically, Flask is a web development framework written in Python, and flask is widely used as a web framework for creating APIs (Application Programming Interfaces). Or we can say it’s a lightweight web application framework that is classified as a microframework. 

Flask has some functionality like tools, libraries, and technologies that allow you to build a web application. And Web applications frequently require processing to Get Data requests from users.

Python- Flask Request 

In Python-Flask the request module is an object that allows you to access the data sent from the user to the server (Client-Server). and that data is passed into your Flask Application

So there are two types of methods that can be used to recover the data:

  • GET Method
  • POST Method

Make sure that in your IDE or VS code editor Flask web framework should be installed. using the following command you can install-

pip install Flask
pip install flask-requests

and then we will import the request module using the following line of code.

Python3




# import the flask module in python
from flask import Flask, request


Get the Data Received in a Flask request 

To perform Get the Data Received 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. 

  1. Accesses the Query String ( URL parameters).
  2. Accesses the Form Data. 
  3. Returned the JSON data.
  4. Returned the Dictionary Cookies.

Accesses the Query String Parameter 

Step 1: After importing the flask, request module, we will call the Flask-Application flask constructor call the name of the current module (__name__) as an argument. using the following line of code.

Python3




# Get the data using request object in flask.
from flask import Flask, request
app = Flask(__name__)


Step 2: Now we have to use the Visual Studio code editor and made the app.py file and write the code and run. 

Python3




# code
from flask import Flask, request
# flask name module
app = Flask(__name__)
 
# create routes for call query example
@app.route('/query_example')
def query_example():
    request
    return 'Hello GeeksForGeeks...'
 
if __name__ == "__main__":
    app.run(debug=True)


After running just simply go to any browser and write the below command as the name of route.

http://127.0.0.1:5000/query_example?

Output:

 

Step 4: Now we will just modify the line of code and going to allow this route to read in any of the query parameters, for that use the request object and call the args.get and pass get. we will make as a html header tag.

Note- when we update the line of code in the editor and then run it on the browser we need to change the key in the browser search bar. like according to the language, it will show that.

Python3




# code using with request argument.
from flask import Flask, request
app = Flask(__name__)
 
 
@app.route('/query_example')
def query_example():
    language = request.args.get('language')
    return '<h1> The Language is : {GeeksForGeeks} </h1>'.format(language)
 
 
if __name__ == '__main__':
    app.run(debug=True, port=5000)


Now that we have done, we will just Create the key ‘language’ and value as ‘python’.

http://127.0.0.1:5000/query_example?language=python

Output:

 

In the above, if we change the language then we have put the key ‘language’ and value as ‘Ruby’.

http://127.0.0.1:5000/query_example?language=Ruby

 

Step 5: So, now we can also represent the many keys, values, framework and website. just implement the line of code.

Python3




def query_example():
    language = request.args.get('language')
    framework = request.args['framework']
    website = request.args.get('website')
    return '''<h1> The Language is : {} </h1>
              <h1> The framework is : {} </h1>
              <h1> The website is : {}  </h1>'''.format(language,
                                                        framework,
                                                        website)


After run the code enter the following command.

http://127.0.0.1:5000/query_example?language=PHP&framework=Flask&website=flask.org

Output:

 

Accesses the form data:

Step 1: So, in this accesses the form data, we will use the form example here with POST method and create the web form on a browser, for performing that we need to create a route and function. And remaining code as it is the same, following the line of code.

Python3




# create route for form and function.
from flask import Flask, request
# flask name module
app = Flask(__name__)
 
 
@app.route('/form_example')
def form_example():
    return '''<form method="POST" action="">
    Language <input types="text" name="language">
    Framework <input type="text" name="framework">
    <input type="submit">
    </form>'''


After running just simply go to any browser and write the below command as the name of route.

http://127.0.0.1:5000/form_example

Output:

 

Step 2: Now what we have to do we will use POST and GET methods as well as conditions, to show the language and which framework we are using. just implement the line of code.

Python3




# Use the POST and GET method for creative form.
@app.route('/form_example', methods=['POST', 'GET'])
def form_example():
    if request.method == 'POST':
        language = request.form.get('language')
        framework = request.form['framework']
        return '<h1> The language is {}. The framework is {}.</h1>'.format(language, framework)
    return '''<form method="POST" action="">
    Language <input types="text" name="language">
    Framework <input type="text" name="framework">
    <input type="submit">
    </form>'''


When you will fill out the form in the language we write python and in framework, we write the Flask, by submitting the form I am able to read the foreign data in post request and display it to the user.

Output- 

 

Accesses the JSON Data

Step 1: Now we take the JSON data which is normally constructed by a process that calls the route. And create the function. 

Python3




# create the route for call JSON Data
@app.route('/json_example')
def json_example():
    return 'Hey GeeksForGeeks'


After running just simply go to any browser and write the below command as the name of the route.

http://127.0.0.1:5000/json_example

Output:

 

Step 2: Now for performing the JSON data we need the software ‘Postman’ which performance is automated. After then you have to just copy the browser JSON data link and paste it into postman. then you will get that output.

 

Step 3: Now we will use the POST method in JSON(JavaScript Object Notation) data, following line of code.

Python3




# code
@app.route('/json_example', methods=['POST'])
def json_example():
    return 'Hey GeeksForGeeks'


Step 4: Now in this step we will do 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 JSON object in postman.

JSON Object Data Code In Postman

{
   “language” : “Python”,
   “framework” : “Flask”,
   “website” : “scotch”,
   “version_info” : {
       “python” : 3.7,
       “flask” : 1.0
   },
   “example” : [ “query”, “form”, “json”],
   “boolean_test” : true
}

 

Step 5: Now in this step we will implement the JSON code and add all the JSON Object code in the Python file.

Python3




# code
@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 '''<h1>
    The language value is {}.
    The framework value is {}.
    The python version is {}.
    The example at 0 index is {}.
    The boolean value is {}.
    </h1>'''.format(language, framework, python_version,
                    example, boolean_test)


Output:

 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments