📖 Scenario: You are building a simple REST API for a library system. The API manages books, and you want to allow users to remove books from the collection when they are no longer available.
🎯 Goal: Build a REST API endpoint that allows deleting a book resource by its ID using the DELETE HTTP method.
📋 What You'll Learn
Create a dictionary called books with exact book entries
Add a variable book_id_to_delete to specify which book to remove
Write code to delete the book with the given ID from the books dictionary
Print the updated books dictionary after deletion
💡 Why This Matters
🌍 Real World
APIs often need to remove resources like users, products, or posts. Using DELETE requests is the standard way to do this.
💼 Career
Understanding how to remove resources safely and correctly is essential for backend developers and API designers.
Progress0 / 4 steps
1
DATA SETUP: Create the initial books dictionary
Create a dictionary called books with these exact entries: 1: 'The Hobbit', 2: '1984', 3: 'Pride and Prejudice'
Rest API
Hint
Use curly braces {} to create a dictionary with keys as numbers and values as book titles.
2
CONFIGURATION: Specify the book ID to delete
Create a variable called book_id_to_delete and set it to 2 to indicate the book to remove
Rest API
Hint
Just assign the number 2 to the variable book_id_to_delete.
3
CORE LOGIC: Delete the book from the dictionary
Use the del statement with books[book_id_to_delete] to remove the book with the given ID
Rest API
Hint
Use del followed by the dictionary key access to remove the entry.
4
OUTPUT: Print the updated books dictionary
Write print(books) to display the dictionary after deletion
Rest API
Hint
Use the print() function to show the current state of the books dictionary.
Practice
(1/5)
1. What is the main purpose of the HTTP DELETE method in REST APIs?
easy
A. To remove a resource identified by a URL
B. To create a new resource on the server
C. To update an existing resource
D. To retrieve data from the server
Solution
Step 1: Understand HTTP methods
HTTP methods define actions on resources. DELETE is specifically for removal.
Step 2: Identify DELETE's role
DELETE removes the resource at the given URL, unlike POST (create), PUT/PATCH (update), or GET (retrieve).
Final Answer:
To remove a resource identified by a URL -> Option A
Quick Check:
DELETE = remove resource [OK]
Hint: DELETE always removes resource at the URL, no body needed [OK]
Common Mistakes:
Confusing DELETE with GET or POST
Thinking DELETE requires a request body
Assuming DELETE creates or updates data
2. Which of the following is the correct syntax to send a DELETE request to remove a user with ID 123 using curl?
easy
A. curl -X GET https://api.example.com/users/123
B. curl -X PUT https://api.example.com/users/123
C. curl -X POST https://api.example.com/users/123
D. curl -X DELETE https://api.example.com/users/123
Solution
Step 1: Identify HTTP method for deletion
DELETE method is used to remove resources, so -X DELETE is correct.
Step 2: Check URL and method usage
The URL targets user 123, and curl uses -X to specify method. So, curl -X DELETE URL is correct.
Final Answer:
curl -X DELETE https://api.example.com/users/123 -> Option D
Quick Check:
curl DELETE syntax = curl -X DELETE URL [OK]
Hint: Use curl -X DELETE followed by resource URL [OK]