Understanding HTTP methods
Understanding HTTP methods is crucial when you're working with Flask RESTful APIs. These methods, also known as HTTP verbs, tell the server what action to take. For this article, we'll look at the four most common ones: GET, POST, PUT, and DELETE.
HTTP Methods
GET
The GET method is used to retrieve data from a server at a specified resource. When making a GET request, the server responds with the data requested for that specific resource. The URL contains all the necessary information the server needs to find and return the resource.
Here is an example of a GET request in Flask:
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/api/resource', methods=['GET'])
def get_resource():
data = {"key": "value"} # This could be any data you want to send.
return jsonify(data), 200
In this example, we're defining a route /api/resource
that listens for GET requests. When a GET request is made to this endpoint, the function get_resource()
is called, and it returns a JSON response.
POST
The POST method is used to send data to a server to create a new resource. The data sent to the server with the POST method is stored in the request body of the HTTP request.
Here is an example of a POST request in Flask:
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/api/resource', methods=['POST'])
def create_resource():
if not request.json:
return jsonify({"message": "Bad Request"}), 400
data = request.json
# Here you would usually process the data and create a new resource.
return jsonify(data), 201
In this example, we're defining a route /api/resource
that listens for POST requests. When a POST request is made to this endpoint, the function create_resource()
is called.
PUT
The PUT method is used to update an existing resource. The data sent to the server with the PUT request is stored in the request body of the HTTP request.
Here is an example of a PUT request in Flask:
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/api/resource/<id>', methods=['PUT'])
def update_resource(id):
if not request.json:
return jsonify({"message": "Bad Request"}), 400
data = request.json
# Here you would usually process the data and update the resource.
return jsonify(data), 200
In this example, we're defining a route /api/resource/<id>
that listens for PUT requests. When a PUT request is made to this endpoint, the function update_resource()
is called.
DELETE
The DELETE method is used to delete an existing resource. The server completely removes the specified resource.
Here is an example of a DELETE request in Flask:
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/api/resource/<id>', methods=['DELETE'])
def delete_resource(id):
# Here you would usually delete the resource.
return jsonify({"message": "Resource Deleted"}), 200
In this example, we're defining a route /api/resource/<id>
that listens for DELETE requests. When a DELETE request is made to this endpoint, the function delete_resource()
is called.
Conclusion
These are the four most common HTTP methods that you'll use when building RESTful APIs with Flask. Understanding how they work will help you design and build effective and efficient APIs. Remember, each method has a specific purpose and should only be used for that intended purpose to ensure your API works as expected. Always make sure to return the appropriate HTTP status codes in your responses to help the client understand what happened with their request.