Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

FastAPI PUT and DELETE Requests

00:00 In this lesson, you’ll learn how to correct the number of sides in the dodecagon shape using your RestAPI, and you’ll also use the API to delete documents too. According to the REST standard, updates should be done using requests with the PUT HTTP method.

00:17 Just like with GET and POST, there’s a decorator for that. Since you want to update a specific document, the endpoint includes a shape_id variable that identifies the document requiring the update. In addition, you’ll need to supply some data in the @put body telling Mongita how to update the document. Therefore, the handler function for the PUT request will accept a Shape model class.

00:42 The function signature looks like this. Inside of the update_shape body, you should first check to see if a document with the ID exists. The count_documents() method of the collection will return the number of documents meeting the criteria of a filter document. The filter document to find a document with a specific ID looks like this.

01:06 Instead of updating specific fields, for brevity you’ll replace the entire document with a new one. The result will be the same. The .replace_one() method of the collection accepts the filter document, which again will look for documents with a specific ID and select the first.

01:23 The second value is the replacement document, which is the new shape. Remember to call the .dict() method on the shape object. And finally return that shape.

01:36 If there is no document with the shape_id, raise an HTTPException with the status code 404 for “not found.”

01:52 There is another option for the update. Mongita supports the idea of an upsert. In other words, if an update is attempted for a document that does not exist, the replacement document will be created automatically.

02:04 Thus, upsert is a portmanteau of “update” and “insert.” To perform the upsert, add the upsert keyword argument to the .replace_one() method with a value of true.

02:20 You can now remove the code to check if the document exists.

02:38 Head back to the browser and refresh the documentation. Look for the PUT method to update the shape. Click the Try it out button and fill out the JSON, this time with the number of sides being 12.

02:59 Click the Execute button and see the returned shape with the correct number of sides this time. Go to the GET method for all shapes, execute it, and see that the change has been committed and the database reflects the dodecagon with twelve sides.

03:22 Just one more method left to finish the API: the DELETE method. Again, there is a @delete decorator that takes a path.

03:39 Use the .delete_one method of the shapes collection to attempt to delete the shape. This method just accepts a filter document.

03:48 The .delete_one() method returns a delete result with a deleted_count property. The deleted_count is the number of documents removed, if any. Thus, if no document with the shape_id exists, the deleted_count is 0 and no error is raised, so you could stop here and return a value indicating that there was no error.

04:07 Or you could check the deleted_count and raise 404 if no shapes were found.

04:19 Refresh the docs in the browser. Look for the DELETE method, click the Try it out button, and enter a shape_id of 2 for the dodecagon.

04:30 Click the Execute button and see the status of the request is 200, and the returned JSON indicates the delete was completed. Go back to the GET method to list all shapes. Click the Try it out button. Click the Execute button.

04:51 And the dodecagon has been removed. That’s it. Your API is complete. In the last lesson of the course, you’ll review what you have learned.

Become a Member to join the conversation.