Skip to main content
In this lesson, we complete the CRUD operations by implementing the update functionality using the HTTP PUT method. Building on the DELETE operation structure, this guide explains how to update a post by sending a complete payload that follows the defined schema.

Setting Up a New Request in Postman

Begin by creating a new request in Postman using the same URL structure as for deleting a post (e.g., /posts/{id}). When updating a post, your request body must include all data fields defined in the post schema: title, content, a published flag, and an optional rating. For instance, sending only a title like the example below will not work with the PUT method because it expects data for every field:
Instead, when updating the title, the complete request body should look like:

Understanding the FastAPI Application

The following snippet shows a basic FastAPI application that defines a Post model and stores posts in a list. Notice the helper functions used to locate posts by their ID.
Ensure that the Postman request body is set as raw JSON. The initial test might just update a single field, but remember to include all required fields for a successful update.

Testing the Update Operation in Postman

When testing in Postman, if you attempt to update only one field with:
you will encounter issues because the PUT method requires the complete post data. For example, if the original Post 1 is structured as:
Then, to update Post 1, your request body must include all required fields:

The DELETE Operation for Context

Below is the DELETE endpoint implementation that removes a post based on its ID:

Implementing the PUT Operation

The new PUT operation checks whether the post exists using the find_index_post helper function. If the post is not found, it raises a 404 error; otherwise, it updates the post by replacing the old values with the new data while preserving the original ID.
When you run the application, your terminal will log information similar to the following, confirming that the update operation is working:

Verifying the Update

After updating a post (for example, changing the title to “updated title” and content to “This is the new content”), you should receive a response like:
To confirm, perform a GET request on /posts. The updated list of posts should display the modified data:
Review the JSON response in Postman to ensure every post object includes all the expected fields. This helps verify that the data is persisted correctly.
The image shows the Postman application interface with a GET request to retrieve posts from a local server. The response is displayed in JSON format, showing data for multiple posts.

Next Steps

After confirming the update operation, all CRUD operations have been successfully implemented in the FastAPI application. In the next lesson, we will refactor the code for enhanced readability and maintainability before integrating a PostgreSQL database to persist the data. Transcribed by https://otter.ai

Watch Video