0
0
FastAPIframework~10 mins

Route decorator syntax in FastAPI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a GET route for the path '/items'.

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.[1]("/items")
def read_items():
    return {"items": [1, 2, 3]}
Drag options to blanks, or click blank then click option'
Apost
Bdelete
Cput
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using @app.post instead of @app.get for a GET route.
Forgetting to add parentheses after the decorator name.
2fill in blank
medium

Complete the code to define a POST route for the path '/users'.

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.[1]("/users")
def create_user():
    return {"message": "User created"}
Drag options to blanks, or click blank then click option'
Apost
Bget
Cdelete
Dpatch
Attempts:
3 left
💡 Hint
Common Mistakes
Using @app.get instead of @app.post for creating resources.
Misspelling the decorator name.
3fill in blank
hard

Fix the error in the route decorator to correctly define a DELETE route for '/items/{item_id}'.

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.[1]("/items/{item_id}")
def delete_item(item_id: int):
    return {"message": f"Item {item_id} deleted"}
Drag options to blanks, or click blank then click option'
Apost
Bdelete
Cget
Dput
Attempts:
3 left
💡 Hint
Common Mistakes
Using @app.get or @app.post instead of @app.delete for deletion.
Forgetting to include the path parameter in the route.
4fill in blank
hard

Fill both blanks to define a PUT route for updating an item at '/items/{item_id}'.

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.[1]("/items/[2]")
def update_item(item_id: int):
    return {"message": f"Item {item_id} updated"}
Drag options to blanks, or click blank then click option'
Aput
Bpost
Citem_id
Ditems
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'items' instead of 'item_id' in the path parameter.
Using @app.post instead of @app.put for updates.
5fill in blank
hard

Fill all three blanks to define a PATCH route for partially updating a user at '/users/{user_id}'.

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.[1]("/users/[2]")
def patch_user([3]: int):
    return {"message": f"User {user_id} patched"}
Drag options to blanks, or click blank then click option'
Apatch
Buser_id
Dusers
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for path and function parameters.
Using @app.put instead of @app.patch for partial updates.