📖 Scenario: Imagine you are building a simple web service for a library. The library wants to manage its books online. Each action you take on the books, like adding a new book, checking details, updating information, or removing a book, needs to be clear and easy to understand by the web service.
🎯 Goal: You will create a small program that shows how different HTTP methods (GET, POST, PUT, DELETE) clearly express what you want to do with the books. This helps the web service know your intent and respond correctly.
📋 What You'll Learn
Create a dictionary called books with three books.
Create a variable called book_id set to 2.
Write code that uses the HTTP method GET to show the book details for book_id.
Print the result showing the HTTP method used and the book details.
💡 Why This Matters
🌍 Real World
Web services use HTTP methods to know what users want to do, like reading or changing data.
💼 Career
Knowing HTTP methods is essential for jobs in web development, API design, and backend programming.
Progress0 / 4 steps
1
DATA SETUP: Create the 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 1, 2, 3 and their book titles as values.
2
CONFIGURATION: Set the book ID to access
Create a variable called book_id and set it to 2.
Rest API
Hint
Just assign the number 2 to the variable book_id.
3
CORE LOGIC: Use GET method to fetch book details
Write code that uses the HTTP method GET to get the book title from books using book_id. Store the result in a variable called result as a string in this format: 'GET book: [book title]'.
Rest API
Hint
Use an f-string to combine 'GET book: ' with the book title from the dictionary using book_id.
4
OUTPUT: Print the result
Write a print statement to display the value of result.
Rest API
Hint
Use print(result) to show the output.
Practice
(1/5)
1. Which HTTP method is used to retrieve data from a server without changing it?
easy
A. GET
B. POST
C. DELETE
D. PUT
Solution
Step 1: Understand the purpose of HTTP methods
GET is designed to request data without modifying it, making it safe and idempotent.
Step 2: Match method to action
Since the question asks for retrieving data without changes, GET fits perfectly.
Final Answer:
GET -> Option A
Quick Check:
Retrieve data = GET [OK]
Hint: GET is for reading data, no changes made [OK]
Common Mistakes:
Confusing GET with POST
Thinking DELETE retrieves data
Using PUT to fetch data
2. Which of the following is the correct syntax to update a resource using HTTP methods?
easy
A. GET /resource/123
B. DELETE /resource/123
C. POST /resource/123
D. PUT /resource/123
Solution
Step 1: Identify the method used for updating
PUT is the HTTP method used to update or replace a resource at a specific URL.
Step 2: Check the syntax correctness
PUT /resource/123 correctly targets the resource with ID 123 for update.
Final Answer:
PUT /resource/123 -> Option D
Quick Check:
Update resource = PUT [OK]
Hint: PUT updates existing resources fully [OK]
Common Mistakes:
Using GET to update
POST is for creating, not updating
DELETE removes, not updates
3. What will be the expected result of this HTTP request?
POST /users
{
"name": "Alice",
"age": 30
}
medium
A. Create a new user named Alice
B. Delete user Alice
C. Update existing user Alice's age
D. Retrieve user data for Alice
Solution
Step 1: Identify the HTTP method used
POST is used to create new resources on the server.
Step 2: Analyze the request body
The JSON data contains user details, indicating creation of a new user named Alice.
Final Answer:
Create a new user named Alice -> Option A
Quick Check:
POST with data = Create new resource [OK]
Hint: POST adds new data, not fetch or delete [OK]
Common Mistakes:
Thinking POST retrieves data
Confusing POST with PUT for update
Assuming POST deletes data
4. You want to remove a user with ID 45 using an HTTP request, but the request POST /users/45 is not working. What is the correct method to use?
medium
A. GET
B. PUT
C. DELETE
D. PATCH
Solution
Step 1: Understand the intent of the request
The goal is to remove (delete) a user, so the method must express deletion.
Step 2: Choose the HTTP method for deletion
DELETE is the standard method to remove a resource identified by a URL.
Final Answer:
DELETE -> Option C
Quick Check:
Remove resource = DELETE [OK]
Hint: DELETE removes resources, POST does not [OK]
Common Mistakes:
Using POST to delete
Confusing PATCH with DELETE
Trying GET to remove data
5. You have an API where PATCH /profile/123 updates only the email field of a user profile, while PUT /profile/123 replaces the entire profile. Why is it important to choose PATCH over PUT when updating just one field?
hard
A. PATCH is faster because it sends less data
B. PUT can accidentally erase other fields if not included
C. PATCH creates a new profile instead of updating
D. PUT is only for deleting resources
Solution
Step 1: Understand the difference between PUT and PATCH
PUT replaces the entire resource, so missing fields get erased. PATCH updates only specified fields.
Step 2: Analyze the risk of using PUT for partial updates
If you use PUT but omit some fields, those fields may be removed unintentionally.
Final Answer:
PUT can accidentally erase other fields if not included -> Option B
Quick Check:
Partial update safe = PATCH, full replace risk = PUT [OK]
Hint: Use PATCH for partial updates to avoid data loss [OK]
Common Mistakes:
Thinking PATCH creates new resource
Believing PUT only deletes
Assuming PATCH sends no data
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.