Removing Items with DELETE Requests
Removing Items with DELETE Requests in FastAPI
What an amazing progress you did! So far, we have practiced making GET, POST, and PUT requests, but there's more to HTTP methods. Let's start our discussion on the HTTP DELETE method.
Just like our previous lessons, every HTTP method has a unique role, and understanding when and why to use each one is a fundamental web development skill. As the name suggests, the DELETE method is specifically used to remove a particular resource from the server.
FastAPI Recap
Before we jump into exploring the DELETE method, let's quickly look at how we set up our FastAPI application and our mock directory of crew members, which we will be using again in this lesson:
Outcome Overview
Our task in this lesson is to create an endpoint using the DELETE method to remove an item from our mock crew database. This deletion allows us to manage our data, keeping it consistent and updated, which is very important in real-world applications, such as updating the list of crew members on a spaceship after a member leaves.
Building a DELETE Endpoint
Now, let's dive into the code and start building our DELETE endpoint. To create a DELETE endpoint, we simply need to change the decorator to @app.delete and update what the function does to handle the deletion logic.
This endpoint will receive the crew_id as a parameter from the URL, and that ID will be the identifier we will use to find and remove the crew member from the database.
Understanding the Process
