0
0
FastAPIframework~10 mins

Path operations (GET, POST, PUT, DELETE) 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 create a GET path operation that returns a welcome message.

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.[1]("/")
async def read_root():
    return {"message": "Welcome to FastAPI!"}
Drag options to blanks, or click blank then click option'
Aget
Bpost
Cput
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST instead of GET for retrieving data.
2fill in blank
medium

Complete the code to create a POST path operation that accepts an item name and returns it.

FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    name: str

@app.[1]("/items/")
async def create_item(item: Item):
    return {"item_name": item.name}
Drag options to blanks, or click blank then click option'
Apost
Bget
Cput
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET instead of POST when sending data to create.
3fill in blank
hard

Fix the error in the code to update an item using PUT method.

FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    name: str

@app.[1]("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    return {"item_id": item_id, "updated_name": item.name}
Drag options to blanks, or click blank then click option'
Apost
Bdelete
Cget
Dput
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST or GET instead of PUT for updates.
4fill in blank
hard

Fill both blanks to create a DELETE path operation that deletes an item by its ID.

FastAPI
from fastapi import FastAPI

app = FastAPI()

@app.[1]("/items/[2]")
async def delete_item(item_id: int):
    return {"message": f"Item {item_id} deleted"}
Drag options to blanks, or click blank then click option'
Adelete
Bpost
C{item_id}
D/item_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST or GET instead of DELETE.
Using incorrect path parameter syntax.
5fill in blank
hard

Fill all three blanks to create a PUT path operation that updates an item and returns its ID and new name.

FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    name: str

@app.[1]("/items/[2]")
async def update_item([3]: int, item: Item):
    return {"item_id": [3], "new_name": item.name}
Drag options to blanks, or click blank then click option'
Aput
Bitem_id
C{item_id}
Dpost
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching path parameter names.
Using POST instead of PUT.