Path operations (GET, POST, PUT, DELETE) with FastAPI
📖 Scenario: You are building a simple API to manage a list of books in a library. Each book has an id, title, and author. You want to allow users to get the list of books, add a new book, update an existing book, and delete a book.
🎯 Goal: Create a FastAPI app with four path operations: GET to list all books, POST to add a new book, PUT to update a book by id, and DELETE to remove a book by id.
📋 What You'll Learn
Create a list called books with initial book entries
Add a variable app as a FastAPI instance
Create a GET path operation at /books/ to return all books
Create a POST path operation at /books/ to add a new book
Create a PUT path operation at /books/{book_id} to update a book by book_id
Create a DELETE path operation at /books/{book_id} to delete a book by book_id
💡 Why This Matters
🌍 Real World
APIs like this are used in web services to manage data such as books, users, or products. Understanding path operations is essential for building RESTful APIs.
💼 Career
FastAPI is a popular framework for backend development. Knowing how to create GET, POST, PUT, and DELETE endpoints is a core skill for backend developers and API engineers.
Progress0 / 4 steps
1
Create initial book data
Create a list called books with these exact dictionaries: {'id': 1, 'title': '1984', 'author': 'George Orwell'} and {'id': 2, 'title': 'Brave New World', 'author': 'Aldous Huxley'}.
FastAPI
Hint
Use a list with two dictionaries exactly as shown.
2
Create FastAPI app instance
Import FastAPI from fastapi and create a variable called app as an instance of FastAPI().
FastAPI
Hint
Use from fastapi import FastAPI and then app = FastAPI().
3
Add GET and POST path operations
Add a @app.get("/books/") function called get_books that returns the books list. Then add a @app.post("/books/") function called add_book that accepts a book dictionary parameter and appends it to books.
FastAPI
Hint
Use @app.get("/books/") and @app.post("/books/") decorators with functions named get_books and add_book.
4
Add PUT and DELETE path operations
Add a @app.put("/books/{book_id}") function called update_book that takes book_id and book dictionary, updates the matching book in books. Add a @app.delete("/books/{book_id}") function called delete_book that removes the book with matching book_id from books.
FastAPI
Hint
Use @app.put("/books/{book_id}") and @app.delete("/books/{book_id}") with functions named update_book and delete_book. Use a for loop with enumerate to find the book by id.
Practice
(1/5)
1. Which HTTP method in FastAPI is typically used to retrieve data without changing anything on the server?
easy
A. DELETE
B. POST
C. PUT
D. GET
Solution
Step 1: Understand HTTP methods purpose
GET is used to read or retrieve data without modifying it.
Step 2: Match method to FastAPI operation
FastAPI uses @app.get() decorator for GET requests to fetch data.
Final Answer:
GET -> Option D
Quick Check:
GET = Read data [OK]
Hint: GET always fetches data without side effects [OK]
Common Mistakes:
Confusing GET with POST which creates data
Using PUT for reading data
Thinking DELETE retrieves data
2. Which of the following is the correct syntax to define a POST path operation in FastAPI to create a new item?
easy
A. @app.post('/items/')\ndef create_item(): pass
B. @app.get('/items/')\ndef create_item(): pass
C. @app.put('/items/')\ndef create_item(): pass
D. @app.delete('/items/')\ndef create_item(): pass
Solution
Step 1: Identify the HTTP method for creating data
POST is used to create new resources in REST APIs.
Step 2: Match FastAPI decorator for POST
FastAPI uses @app.post() decorator to handle POST requests.
Final Answer:
@app.post('/items/')\ndef create_item(): pass -> Option A
Quick Check:
POST = Create data [OK]
Hint: Use @app.post() for creating new data [OK]
Common Mistakes:
Using @app.get() for creating data
Confusing PUT with POST
Using DELETE for creation
3. What will be the response when this FastAPI code is called with a PUT request to update item with id 5?
The code deletes an item by key without verifying if the key exists in the dictionary.
Step 2: Understand possible runtime error
If the item_id does not exist, del will raise a KeyError causing a server error.
Final Answer:
Deleting item without checking if it exists -> Option C
Quick Check:
Delete needs existence check [OK]
Hint: Always check if item exists before deleting [OK]
Common Mistakes:
Assuming item always exists
Ignoring KeyError on delete
Thinking async is wrong here
5. You want to create a FastAPI app that supports all four main HTTP methods (GET, POST, PUT, DELETE) for managing a list of users stored in a dictionary. Which approach correctly handles updating a user only if it exists, creating a new user, retrieving a user by ID, and deleting a user safely?
hard
A. Use @app.get to retrieve, @app.post to create, @app.put to update without checking existence, and @app.delete to delete without checking existence
B. Use @app.get to retrieve, @app.post to create, @app.put to update with existence check, and @app.delete to delete with existence check
C. Use only @app.post for all operations to simplify code
D. Use @app.put to create and update, @app.get to delete, @app.post to retrieve
Solution
Step 1: Match HTTP methods to CRUD operations
GET retrieves, POST creates, PUT updates, DELETE removes resources.
Step 2: Ensure safe update and delete
Update and delete should check if the user exists to avoid errors.
Final Answer:
Use @app.get to retrieve, @app.post to create, @app.put to update with existence check, and @app.delete to delete with existence check -> Option B
Quick Check:
Correct HTTP methods with safety checks [OK]
Hint: Match HTTP verbs to actions and check existence before update/delete [OK]
Common Mistakes:
Skipping existence checks causing errors
Using wrong HTTP methods for actions
Trying to do all actions with one method
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.