0
0
FastAPIframework~10 mins

Path parameters 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 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'
A"{item_id}"
B"item_id"
C"<item_id>"
D"[item_id]"
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.
2fill in blank
medium

Complete 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'
Aint
Bfloat
Cstr
Dbool
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'int' when the parameter should be a string.
Omitting the type annotation.
3fill in blank
hard

Fix 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'
A"order_id"
B"[order_id]"
C"<order_id>"
D"{order_id}"
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes without braces around the parameter name.
Using angle or square brackets instead of curly braces.
4fill in blank
hard

Fill 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'
A{category}
Bint
Cstr
Dfloat
Attempts:
3 left
💡 Hint
Common Mistakes
Not using curly braces in the route path.
Using the wrong type annotation like 'int' or 'float'.
5fill in blank
hard

Fill 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'
A{product_id}
Bproduct_id
Cint
Dstr
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatch between path parameter name and function parameter name.
Wrong type annotation like 'str' instead of 'int'.