Complete the code to define a GET route for the path '/items'.
from fastapi import FastAPI app = FastAPI() @app.[1]("/items") def read_items(): return {"items": [1, 2, 3]}
The @app.get decorator defines a GET route in FastAPI. It tells FastAPI to run the function when a GET request is made to the specified path.
Complete the code to define a POST route for the path '/users'.
from fastapi import FastAPI app = FastAPI() @app.[1]("/users") def create_user(): return {"message": "User created"}
The @app.post decorator defines a POST route, which is used to create new resources like users.
Fix the error in the route decorator to correctly define a DELETE route for '/items/{item_id}'.
from fastapi import FastAPI app = FastAPI() @app.[1]("/items/{item_id}") def delete_item(item_id: int): return {"message": f"Item {item_id} deleted"}
The @app.delete decorator defines a DELETE route, which is used to remove resources identified by item_id.
Fill both blanks to define a PUT route for updating an item at '/items/{item_id}'.
from fastapi import FastAPI app = FastAPI() @app.[1]("/items/[2]") def update_item(item_id: int): return {"message": f"Item {item_id} updated"}
The @app.put decorator defines a PUT route for updating resources. The path parameter {item_id} specifies which item to update.
Fill all three blanks to define a PATCH route for partially updating a user at '/users/{user_id}'.
from fastapi import FastAPI app = FastAPI() @app.[1]("/users/[2]") def patch_user([3]: int): return {"message": f"User {user_id} patched"}
The @app.patch decorator defines a PATCH route for partial updates. The path parameter {user_id} and function parameter user_id must match.