Understanding Python Flask: A Beginner’s Guide to GET and POST Requests – Session 9

Flask is a lightweight web framework in Python that allows developers to build web applications quickly and with minimal code. It’s an ideal framework for beginners due to its simplicity and flexibility. Flask allows you to handle HTTP requests and build APIs effortlessly.

What is Flask?

Flask is classified as a micro-framework because it doesn’t come with built-in libraries for handling databases, forms, or authentication. Instead, it provides the essentials for web application development, and you can add libraries and tools as needed. Flask is a great starting point for building RESTful APIs and web applications.

Setting Up Flask

Before diving into GET and POST methods, let’s first set up a basic Flask environment. To begin, you need to install Flask. Open your terminal or command prompt and run:

pip install flask

Once installed, create a new Python file, say app.py. In this file, you can define your Flask app:

from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello, Flask!"

if __name__ == '__main__':
    app.run(debug=True)

This code sets up a basic Flask app and defines a single route (URL path) that returns a “Hello, Flask!” message. To run the app, type:

python app.py

The app will start running, and you can visit http://127.0.0.1:5000/ in your browser to see the message.

Understanding GET and POST Requests

In web development, there are several HTTP methods that a server can respond to. The two most common methods are:

  1. GET: Used to request data from a server. This method retrieves information without making any changes.
  2. POST: Used to send data to the server, often for creating or updating resources.

GET Request

A GET request is typically used to fetch data from a server. Let’s add a simple GET request to our Flask app:

@app.route('/get_data', methods=['GET'])
def get_data():
    sample_data = {"message": "This is a GET request"}
    return jsonify(sample_data)

In this example, when a GET request is made to /get_data, the server responds with a JSON message. You can test this by visiting http://127.0.0.1:5000/get_data in your browser or by using an API tool like Postman.

POST Request

A POST request is used to send data to the server. For example, you can send form data or JSON data. Let’s modify our Flask app to handle a POST request:

@app.route('/post_data', methods=['POST'])
def post_data():
    data = request.get_json()  # Get the JSON data from the request body
    response = {"received": data, "message": "This is a POST request"}
    return jsonify(response)

In this example, when a POST request is made to /post_data, the server retrieves the JSON data sent in the request body and responds with a confirmation message.

To test this, you can use an API tool like Postman to send a POST request to http://127.0.0.1:5000/post_data with a JSON body such as:

{
  "name": "Flask",
  "type": "framework"
}

The server will respond with:

{
  "received": {
    "name": "Flask",
    "type": "framework"
  },
  "message": "This is a POST request"
}

Running and Testing the API

To run your Flask app, simply execute the Python script:

python app.py

Flask will run your application on http://127.0.0.1:5000/ by default. You can test your GET and POST requests using the browser (for GET) or an API testing tool like Postman (for both GET and POST).

Here’s a summary of what we covered:

  • GET request: Retrieve data from the server (visit /get_data).
  • POST request: Send data to the server (use /post_data with JSON data).

Conclusion

Flask is a powerful yet easy-to-use framework that makes it simple to create APIs. With just a few lines of code, you can handle GET and POST requests. This tutorial covers the basics of setting up Flask, handling HTTP requests, and testing your API.

With this foundation, you can start building more complex APIs or web applications using Flask.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x