Lesson 4
Removing Data with DELETE Requests
Removing Data with DELETE Requests

Welcome to another lesson of our course! So far, we have learned how to retrieve data using GET requests, insert new data with POST requests, and update existing records with PUT requests. Today, we'll focus on removing data using DELETE requests.

By the end of this lesson, you'll be able to set up a DELETE endpoint to remove user data from our mock database, handle any potential errors if the user is not found, and return appropriate responses. Let's get started with understanding the significance of DELETE requests in web APIs.

Recap of Setup

Before we dive into DELETE requests, let's quickly revisit our existing setup. We've already configured a Flask application and a mock database of users. Here's the initial setup code for context:

Python
1from flask import Flask, jsonify 2 3# Initialize a Flask app instance 4app = Flask(__name__) 5 6# Mock database as a list of dictionaries 7database = [ 8 {"id": 1, "username": "cosmo"}, 9 {"id": 2, "username": "jake"}, 10 {"id": 3, "username": "emma"} 11]

This setup should be familiar to you from previous lessons.

Understanding DELETE Requests

A DELETE request is used to remove an existing resource from the server. DELETE requests are crucial for web applications that need to manage and clean up data. For example, if a user decides to delete their account, a DELETE request would be sent to the server to remove the user's data.

Now, let's walk through the steps to implement a DELETE request in Flask.

Step 1: Define the DELETE Route

We begin by defining a route in our Flask application that listens for DELETE requests. We'll create an endpoint that takes a user ID as a parameter to specify which user to delete:

Python
1@app.route('/users/<int:user_id>', methods=['DELETE']) 2def delete_user(user_id):
  • The @app.route decorator sets up the endpoint to listen for DELETE requests at /users/<int:user_id>.
  • The <int:user_id> part of the route indicates that the endpoint expects an integer user ID to specify the user to delete.
Step 2: Find and Delete the User

Next, let's implement the logic to find and delete the user from the mock database:

Python
1@app.route('/users/<int:user_id>', methods=['DELETE']) 2def delete_user(user_id): 3 for user in database: 4 if user['id'] == user_id: 5 database.remove(user) 6 return jsonify(message="User deleted") 7 8 return jsonify(error="User not found"), 404
  • We loop through the database list to find the user with the matching user_id.
  • If a match is found, we remove the user from the database and return a JSON message indicating the user has been deleted.
  • If no match is found, we return a 404 Not Found error with an appropriate message.
Accessing the DELETE Endpoint

To delete a user via our DELETE endpoint, the client must send a DELETE request to /users/<user_id>, substituting <user_id> with the actual ID of the user to be removed. If the specified ID exists, the client will receive the following response with a 200 status code:

JSON
1{ 2 "message": "User deleted" 3}

If the user does not exist, the client should receive a 404 status code and the following response:

JSON
1{ 2 "error": "User not found" 3}
Summary and Next Steps

In this lesson, you have learned how to handle DELETE requests to remove data in a Flask application. We covered:

  • The purpose and importance of DELETE requests.
  • Setting up a DELETE endpoint.
  • Writing the logic to delete a user from a mock database.
  • Handling cases where the user to be deleted is not found.
  • Accessing the DELETE endpoint and expected responses.

You've learned how to handle various HTTP methods in Flask to build robust web APIs. Now it's time to solidify your knowledge through hands-on practice. Keep coding and continue learning!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.