Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import FastAPI correctly.
FastAPI
from fastapi import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request instead of FastAPI
Using lowercase fastapi
✗ Incorrect
FastAPI is imported from the fastapi package using FastAPI.
2fill in blank
mediumComplete the code to create a FastAPI app instance.
FastAPI
app = [1]() Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Flask() instead of FastAPI()
Using lowercase fastapi()
✗ Incorrect
You create a FastAPI app by calling FastAPI().
3fill in blank
hardFix the error in the route decorator to define a GET endpoint.
FastAPI
@app.[1]("/") def read_root(): return {"message": "Hello World"}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using post instead of get
Using put or delete for a simple read route
✗ Incorrect
The GET method is used to define a route that responds to HTTP GET requests.
4fill in blank
hardFill both blanks to create a path parameter and return it in the response.
FastAPI
@app.get("/items/[1]") async def read_item([2]: int): return {"item_id": [2]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names in path and function
Using generic names like id or name
✗ Incorrect
The path parameter name in the route and the function argument must match exactly.
5fill in blank
hardFill all three blanks to create a POST endpoint that accepts a JSON body with a Pydantic model.
FastAPI
from pydantic import BaseModel class Item(BaseModel): name: str price: float @app.post("/items/") async def create_item([1]: [2]): return {"item_name": [3].name, "item_price": [3].price}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using BaseModel as type instead of the specific model
Mismatching parameter names
✗ Incorrect
The function parameter name (e.g., item) and its type (Item) must match the Pydantic model. The parameter is used to access the data.