DEV Community

Cover image for Day 2: GET and POST request in Flask
Deepak Raj
Deepak Raj

Posted on • Originally published at codeperfectplus.com on

1

Day 2: GET and POST request in Flask

In this tutorial we will learn how to implement GET and POST request in flask using python 3. for this tutorial, we will use some sample superhero data with the following fields: id, name, email, age, secretIdentity. Users can perform the GET and POST request on this data.

Use can fetch all the superheroes data using the GET request and can add a new superhero using the POST request. we will not use any database for this tutorial. we will use a list of dictionaries to store the data.

from flask import Flask, jsonify, request
# Create a Flask app
app = Flask(__name__)
# sample superhero data
superheroes = [
{
"id": 1,
"name": "Superman",
"email": "superman@krypton.com",
"age": 30,
"secretIdentity": "Clark Kent"
},
{
"id": 2,
"name": "Batman",
"email": "batman@cave.com",
"age": 35,
"secretIdentity": "Bruce Wayne"
}
]
@app.route('/')
def home():
return jsonify({'message': 'superheroes API'})
# GET request
@app.route('/superheroes', methods=['GET'])
def get_superheroes():
return jsonify({'superheroes': superheroes})
# GET request with id
@app.route("/superheroes/<int:id>", methods=['GET'])
def get_superhero(id):
return jsonify({'superhero': superheroes[id-1]})
# POST request
@app.route('/superheroes', methods=['POST'])
def add_superhero():
name = request.form['name']
email = request.form['email']
age = request.form['age']
secretIdentity = request.form['secretIdentity']
get_data = {
"id": len(superheroes)+1,
"name": name,
"email": email,
"age": age,
"secretIdentity": secretIdentity
}
print(get_data)
superheroes.append(get_data)
return jsonify({'superheroes': superheroes})
# driver function
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)

Explanation of the code

In the above code, we have created a flask app and we have created a list of dictionaries to store the data. We have created the following routes:

  • / - This is the home route. It will return a message that says superheroes API as a response.
  • /superheroes - This route will return all the superheroes data as a response.\
  • /superheroes/<int:id> - This route will return the superhero data with the given id as a response. so if you want to get the superhero data with id 1 then you have to make a GET request to /superheroes/1.
  • /superheroes - This route will add a new superhero to the list of superheroes. It will take the following data as input: name, email, age, secretIdentity. It will return all the superheroes data as a response.

How to run the flask application?

To run the flask application, you have to run the following command.

python app.py

Enter fullscreen mode Exit fullscreen mode

In the above code, we have used the debug=True parameter. It will automatically reload the server when you make any changes in the code. So you don’t have to restart the server again and again.

How to test the GET and POST request?

You can test the GET and POST request using the Postman application. You can download it from here.

To test the GET request, you have to make a GET request to the following URL: http://localhost:5000/superheroes. It will return all the superheroes data as a response.

To test the GET request with id, you have to make a GET request to the following URL: http://localhost:5000/superheroes/1. It will return the superhero data with id 1 as a response.

To test the POST request, you have to make a POST request to the following URL: http://localhost:5000/superheroes. It will add a new superhero to the list of superheroes. It will take the following data as input: name, email, age, secretIdentity. It will return all the superheroes data as a response.

Conclusion

In this tutorial, we have learned how to implement GET and POST request in flask using python 3. We have also learned how to test the GET and POST request using the Postman application. You can find the complete on my GitHub. If you have any questions or suggestions then you can let me know in the comment section below.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay