Sunday, December 29, 2024
Google search engine
HomeLanguagesGET Request Query Parameters with Flask

GET Request Query Parameters with Flask

In this article, we will learn how we can use the request object in a flask to GET Request Query Parameters with Flask that is passed to your routes using Python.

As we know, Flask is a web development framework written in Python, and the 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.

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-

  1. GET Method
  2. POST Method

Stepwise Implementation 

To perform the “GET Request Query Parameters with Flask”, you have to 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

Accesses the Query Parameter

So starting with the Request object the, thing is that you know about the Request object that contains everything incoming into your endpoint. so basically it contains the incoming data, referrer, IP Address, raw data, and HTTP method and also contains a header.

Step 1:  After installation of the above python module we will import this module in the Python script file. using the following line of code.

Python3




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


Step 2: Once you have imported the flask, request the module then calls the Flask-Application flask constructor and 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 3: Now in the python script file we will introduce the query example for that we will create the routes to call the query parameter.

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’ll just modify the line of code and going to allow the 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 an HTML header tag.

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)


And create the key ‘language’ and value as ‘python’ 

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

Output : 

 

So here, if we change the language then we have put the key ‘language’ and value as ‘Ruby’. follow the given command.

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

Output : 

 

Step 5: Final steps to GET Request and represent the many keys, values, framework and website. just implement the line of code. 

Python3




# Code
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

Create a key ‘framework’ & ‘website’ and their value as ‘Flask’ & ‘flask.org’

Output :

 

Overall these are the steps to perform the GET Request using Query(String) Parameter in Flask web framework via python.  

RELATED ARTICLES

Most Popular

Recent Comments