0
0
FastAPIframework~10 mins

CRUD operations 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 import FastAPI and create an app instance.

FastAPI
from fastapi import [1]

app = [1]()
Drag options to blanks, or click blank then click option'
ARequest
BFastAPI
CResponse
DDepends
Attempts:
3 left
💡 Hint
Common Mistakes
Importing the wrong class like Request or Response.
Forgetting to create the app instance.
2fill in blank
medium

Complete the code to define a GET endpoint that returns a welcome message.

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 a read endpoint.
Forgetting the async keyword.
3fill in blank
hard

Fix the error in the POST endpoint to accept JSON data with a 'name' field.

FastAPI
from pydantic import BaseModel

class Item(BaseModel):
    name: str

@app.post("/items/")
async def create_item(item: [1]):
    return {"item_name": item.name}
Drag options to blanks, or click blank then click option'
Adict
Bstr
Cint
DItem
Attempts:
3 left
💡 Hint
Common Mistakes
Using a plain dict instead of the Pydantic model.
Using wrong types like str or int.
4fill in blank
hard

Fill both blanks to update an item by its ID using PUT method.

FastAPI
@app.[1]("/items/{item_id}")
async def update_item(item_id: int, item: [2]):
    return {"item_id": item_id, "updated_name": item.name}
Drag options to blanks, or click blank then click option'
Aput
Bpost
CItem
Dstr
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST instead of PUT for updates.
Using wrong parameter types.
5fill in blank
hard

Fill all three blanks to delete an item by its ID and return a confirmation message.

FastAPI
@app.[1]("/items/{item_id}")
async def delete_item(item_id: [2]):
    return {"message": f"Item [3] deleted"}
Drag options to blanks, or click blank then click option'
Adelete
Bint
Citem_id
Dstr
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong HTTP method like GET or POST.
Using string type for item_id instead of int.
Not including the item_id in the confirmation message.