📖 Scenario: You are building a simple web service to manage a list of books. Each book has a unique ID and a title. You want to allow users to view all books, add a new book, update an existing book's title, and delete a book.
🎯 Goal: Create a Flask app that handles HTTP methods GET, POST, PUT, and DELETE to manage the books list.
📋 What You'll Learn
Create a dictionary called books with initial book entries
Add a variable called next_id to track the next book ID
Implement Flask routes to handle GET, POST, PUT, and DELETE methods
Use Flask's request object to get JSON data for POST and PUT
Return JSON responses with updated book data
💡 Why This Matters
🌍 Real World
Web services often need to handle different HTTP methods to create, read, update, and delete data. This project shows how to build such a service with Flask.
💼 Career
Understanding HTTP methods and how to implement them in web frameworks like Flask is essential for backend web development jobs.
Progress0 / 4 steps
1
DATA SETUP: Create initial books dictionary
Create a dictionary called books with these exact entries: 1: 'The Hobbit', 2: '1984', and 3: 'Pride and Prejudice'.
Flask
Hint
Use a Python dictionary with integer keys and string values.
2
CONFIGURATION: Add next_id variable
Add a variable called next_id and set it to 4 to track the next book ID.
Flask
Hint
This variable helps assign unique IDs to new books.
3
CORE LOGIC: Implement Flask routes for GET and POST
Import Flask and request from flask. Create a Flask app called app. Add a route /books that accepts GET and POST methods. For GET, return the books dictionary as JSON. For POST, get the title from JSON request data, add it to books with the current next_id, increment next_id, and return the new book entry as JSON.
Flask
Hint
Use @app.route decorator and check request.method.
4
COMPLETION: Add routes for PUT and DELETE to update and delete books
Add a route /books/<int:book_id> that accepts PUT and DELETE methods. For PUT, get the title from JSON request data and update the book with book_id in books. For DELETE, remove the book with book_id from books. Return the updated or deleted book data as JSON.
Flask
Hint
Use route parameters and check request.method for PUT and DELETE.
Practice
(1/5)
1. Which HTTP method is typically used in Flask to retrieve data without changing anything on the server?
easy
A. PUT
B. POST
C. GET
D. DELETE
Solution
Step 1: Understand HTTP methods purpose
GET is used to request data without modifying it, so it is safe and idempotent.
Step 2: Match method to Flask usage
In Flask, GET routes are used to send data back to the client without changing server state.
Final Answer:
GET -> Option C
Quick Check:
Retrieve data = GET [OK]
Hint: GET reads data, POST creates, PUT updates, DELETE removes [OK]
Common Mistakes:
Confusing POST with GET for data retrieval
Using DELETE to get data
Thinking PUT is for reading data
2. Which of the following Flask route definitions correctly allows handling both GET and POST methods?
easy
A. @app.route('/submit', allow_methods=['GET', 'POST'])
B. @app.route('/submit', methods=['GET', 'POST'])
C. @app.route('/submit', methods='GET, POST')
D. @app.route('/submit', method=['GET', 'POST'])
Solution
Step 1: Recall Flask route syntax for methods
Flask uses the 'methods' keyword argument as a list of strings to specify allowed HTTP methods.
Step 2: Check each option's syntax
@app.route('/submit', methods=['GET', 'POST']) correctly uses methods=['GET', 'POST']. Others have wrong keywords or wrong data types.
Final Answer:
@app.route('/submit', methods=['GET', 'POST']) -> Option B
Quick Check:
Correct keyword and list format = @app.route('/submit', methods=['GET', 'POST']) [OK]
Hint: Use methods=['GET', 'POST'] exactly in route decorator [OK]
Common Mistakes:
Using 'method' instead of 'methods'
Passing methods as a string instead of list
Using unknown keywords like 'allow_methods'
3. What will be the output when a client sends a DELETE request to this Flask route?
The route accepts DELETE requests with an integer id parameter and returns a string with status 200.
Step 2: Understand response for DELETE request
Since the method matches and function returns a message with 200, the client receives that message and status.
Final Answer:
"Deleted item <id>" with status 200 -> Option A
Quick Check:
DELETE request handled correctly = "Deleted item <id>" with status 200 [OK]
Hint: Route method matches request method, so response is returned [OK]
Common Mistakes:
Expecting 404 if route exists
Confusing DELETE with GET behavior
Assuming empty response without checking code
4. Identify the error in this Flask route that should handle PUT requests:
@app.route('/update', methods=['PUT'])
def update():
data = request.get_json()
return 'Updated', 200
medium
A. Missing import of 'request' from Flask
B. Incorrect HTTP method name 'PUT'
C. Route decorator syntax is invalid
D. Function must return JSON, not string
Solution
Step 1: Check usage of 'request'
The function uses 'request.get_json()' but 'request' is not imported from Flask, causing a NameError.
Step 2: Verify other parts
Method 'PUT' is valid, route syntax is correct, and returning string with status 200 is allowed.
Final Answer:
Missing import of 'request' from Flask -> Option A
Quick Check:
Using 'request' without import causes error [OK]
Hint: Always import 'request' to access request data [OK]
Common Mistakes:
Forgetting to import 'request'
Thinking PUT is invalid method
Assuming string return is wrong
5. You want to create a Flask API endpoint to update a user's email using PUT and delete the user using DELETE at the same URL '/user/'. Which is the best way to define this in Flask?
hard
A. Use GET method for both update and delete actions
B. Create two separate routes with different URLs for PUT and DELETE
C. Use only POST method and check a hidden field for action
D. Use one route with methods=['PUT', 'DELETE'] and check request.method inside the function
Solution
Step 1: Understand RESTful design
PUT and DELETE are standard methods for update and delete at the same resource URL.
Step 2: Implement in Flask
Flask allows one route to handle multiple methods; inside the function, you check request.method to run correct code.
Final Answer:
Use one route with methods=['PUT', 'DELETE'] and check request.method inside the function -> Option D
Quick Check:
Multiple methods on one route with method check = Use one route with methods=['PUT', 'DELETE'] and check request.method inside the function [OK]
Hint: One route, multiple methods, check request.method inside [OK]
Common Mistakes:
Creating separate URLs for same resource actions
Using POST or GET incorrectly for update/delete
Not checking request.method inside route
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.