Deleting entries
First we need to set up a function that enumerates through all the posts
The above function:
Iterates through all posts in the "database" by using enumerate
This retrieves the index position (
i
) as well as the post (p
)If the post id is the same as the id provided to the function, it returns the index position number (
i
)
Now we can create a
DELETE
Path Operation
This calls the
find_index_post
function we have defined above and provides it with an id of integer type (from decorator)The index number will be "popped" out of the posts
Message with the successful deletion will be returned
Full Code:
If we send now a
DELETE
HTTP request via Postman we should be prompted for a message successfully deleted
If we retrieve all posts from the "database" it should return only the post with ID 2
When deleting entries we should be using HTTP Code 204
So the way FastAPI works is that we should not be sending any data back with a 204
So the new code and HTTP 204 implementation should be:
If we try to return the info, it will work but will throw an error on the backend side:
So what we need to do is grab the response and return no content back
After this there shouldn't be any error anymore
To get a detailed explanation for this click here!
Specifically this comment
We are still running into an issue where if you don't provide a ID that exists it will return a HTTP 500 message
To get around this we need to implement an exception that raises a HTTP 404 message with post does not exist
Last updated