Pets Back End Delete Route

Learning objective: By the end of this lesson, students will be able to create a new route to delete a single pet by interacting with the database and sending a JSON response to the client.

DELETE - Delete route

In this section, we will create a new route to delete a single pet. This route will be a DELETE request to /pets/<pet_id>, returning a status of 204 with no content.

We will be following these specs when building the route:

Defining the route

Inside app.py, create a new route for removing a single pet from the database. Our route should use the pet_id parameter in the route handler function object to access the pet_id from the path. If a pet has been deleted successfully, we’ll return a 204 status, with the message "Pet Deleted Successfully".

Add the following to app.py:

# app.py
@app.route('/pets/<pet_id>', methods=['DELETE'])
def delete_pet(pet_id):
    try:
        connection = get_db_connection()
        cursor = connection.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
        cursor.execute("DELETE FROM pets WHERE id = %s", (pet_id,))
        if cursor.rowcount == 0:
            return "Pet not found", 404
        connection.commit()
        cursor.close()
        return "Pet deleted successfully", 204
    except Exception as e:
        return str(e), 500

With the route in place, let’s test it out in Postman.

Open Postman and create a new request with the following details:

After you have sent the request, you should receive a status code of 204 with no content in the response. This means the pet was successfully deleted from the database.