Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a path parameter named 'item_id' in the route.
FastAPI
from fastapi import FastAPI app = FastAPI() @app.get("/items/[1]") async def read_item(item_id: int): return {"item_id": item_id}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes without braces like 'item_id' instead of '{item_id}'.
Using angle brackets or square brackets instead of curly braces.
✗ Incorrect
In FastAPI, path parameters are defined using curly braces in the route path, like '/items/{item_id}'.
2fill in blank
mediumComplete the code to declare the type of the path parameter 'user_id' as string.
FastAPI
from fastapi import FastAPI app = FastAPI() @app.get("/users/{user_id}") async def read_user(user_id: [1]): return {"user_id": user_id}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'int' when the parameter should be a string.
Omitting the type annotation.
✗ Incorrect
To specify a string path parameter in FastAPI, use 'str' as the type annotation.
3fill in blank
hardFix the error in the path parameter declaration to correctly capture 'order_id' as an integer.
FastAPI
from fastapi import FastAPI app = FastAPI() @app.get("/orders/[1]") async def read_order(order_id: int): return {"order_id": order_id}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes without braces around the parameter name.
Using angle or square brackets instead of curly braces.
✗ Incorrect
Path parameters must be enclosed in curly braces in the route path to be recognized by FastAPI.
4fill in blank
hardFill both blanks to create a path parameter 'category' of type string and return it in the response.
FastAPI
from fastapi import FastAPI app = FastAPI() @app.get("/categories/[1]") async def read_category(category: [2]): return {"category": category}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using curly braces in the route path.
Using the wrong type annotation like 'int' or 'float'.
✗ Incorrect
The path parameter must be in curly braces in the route, and the type annotation should be 'str' for a string.
5fill in blank
hardFill all three blanks to define a path parameter 'product_id' as an integer and return it in a dictionary with key 'product'.
FastAPI
from fastapi import FastAPI app = FastAPI() @app.get("/products/[1]") async def read_product([2]: [3]): return {"product": [2]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatch between path parameter name and function parameter name.
Wrong type annotation like 'str' instead of 'int'.
✗ Incorrect
The path parameter must be in curly braces in the route, the function parameter name must match, and the type should be 'int' for an integer.