Bird
Raised Fist0
FastAPIframework~20 mins

Path parameters in FastAPI - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Path Parameter Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output when accessing a path with a parameter?
Consider this FastAPI code snippet. What will be the response when a client requests /items/42?
FastAPI
from fastapi import FastAPI

app = FastAPI()

@app.get("/items/{item_id}")
async def read_item(item_id: int):
    return {"item_id": item_id}
A{"item_id": "42"}
B{"item_id": 42}
C404 Not Found error
D500 Internal Server Error
Attempts:
2 left
💡 Hint
Remember that FastAPI converts path parameters to the declared type.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a path parameter in FastAPI?
Which of the following FastAPI route definitions correctly declares a path parameter named user_id of type str?
A
@app.get("/users/{user_id}")
async def get_user(user_id: str):
    return {"user_id": user_id}
B
@app.get("/users/user_id")
async def get_user(user_id: str):
    return {"user_id": user_id}
C
@app.get("/users/{user_id:int}")
async def get_user(user_id: str):
    return {"user_id": user_id}
D
@app.get("/users/{user_id}")
async def get_user():
    return {"user_id": user_id}
Attempts:
2 left
💡 Hint
Path parameters must be included in the route string with curly braces and matched in the function parameters.
🔧 Debug
advanced
2:00remaining
Why does this FastAPI path parameter cause a 422 error?
Given this code, why does a request to /products/abc return a 422 Unprocessable Entity error?
FastAPI
from fastapi import FastAPI

app = FastAPI()

@app.get("/products/{product_id}")
async def get_product(product_id: int):
    return {"product_id": product_id}
ABecause the return statement is missing a JSON response model.
BBecause the route path is missing a trailing slash.
CBecause the function is missing an async keyword.
DBecause 'abc' cannot be converted to int as required by the path parameter type.
Attempts:
2 left
💡 Hint
Check the type of the path parameter and the value passed in the URL.
❓ state_output
advanced
2:00remaining
What is the value of user_id after this request?
Given this FastAPI route, what will be the value of user_id inside the function when a client requests /users/007?
FastAPI
from fastapi import FastAPI

app = FastAPI()

@app.get("/users/{user_id}")
async def read_user(user_id: str):
    return {"user_id": user_id}
A"7" (string, leading zeros removed)
B7 (integer, leading zeros removed)
C"007" (string with leading zeros preserved)
DError because of leading zeros in path parameter
Attempts:
2 left
💡 Hint
Check the declared type of the path parameter and how FastAPI treats strings.
🧠 Conceptual
expert
2:00remaining
Which statement about FastAPI path parameters is true?
Select the correct statement about how FastAPI handles path parameters.
AFastAPI automatically converts path parameters to the declared type and returns a 422 error if conversion fails.
BFastAPI treats all path parameters as strings and never performs type conversion.
CFastAPI requires explicit type conversion inside the route function for path parameters.
DFastAPI only supports integer path parameters and rejects others.
Attempts:
2 left
💡 Hint
Think about how FastAPI validates input before calling your function.

Practice

(1/5)
1. What is the purpose of path parameters in FastAPI?
easy
A. To send data only in the request body
B. To define static routes without variables
C. To capture values directly from the URL path
D. To handle only query string parameters

Solution

  1. Step 1: Understand what path parameters do

    Path parameters allow the API to accept dynamic values from the URL path itself, making routes flexible.
  2. Step 2: Compare with other data sources

    Unlike query parameters or request bodies, path parameters are part of the URL path, not optional or in the body.
  3. Final Answer:

    To capture values directly from the URL path -> Option C
  4. Quick Check:

    Path parameters = URL path values [OK]
Hint: Path parameters are always inside curly braces {} in the URL [OK]
Common Mistakes:
  • Confusing path parameters with query parameters
  • Thinking path parameters come from request body
  • Assuming path parameters are optional by default
2. Which of the following is the correct way to declare a path parameter named item_id in FastAPI?
easy
A. @app.get("/items/:item_id")
B. @app.get("/items/item_id")
C. @app.get("/items/<item_id>")
D. @app.get("/items/{item_id}")

Solution

  1. Step 1: Recall FastAPI path parameter syntax

    FastAPI uses curly braces {} to declare path parameters inside route strings.
  2. Step 2: Identify the correct syntax

    Only @app.get("/items/{item_id}") uses curly braces correctly: "/items/{item_id}".
  3. Final Answer:

    @app.get("/items/{item_id}") -> Option D
  4. Quick Check:

    Curly braces {} = path parameters [OK]
Hint: Use curly braces {} around parameter names in route paths [OK]
Common Mistakes:
  • Using quotes or angle brackets instead of curly braces
  • Forgetting to wrap parameter name in braces
  • Using colon syntax like other frameworks
3. Given this FastAPI code snippet, what will be the output when accessing /users/42?
from fastapi import FastAPI
app = FastAPI()

@app.get("/users/{user_id}")
async def read_user(user_id: int):
    return {"user_id": user_id}
medium
A. {"user_id": 42}
B. TypeError because user_id is not a string
C. 404 Not Found error
D. {"user_id": "42"}

Solution

  1. Step 1: Understand parameter type conversion

    The path parameter user_id is declared as int, so FastAPI converts the string "42" from the URL to integer 42.
  2. Step 2: Check the returned JSON

    The function returns a dictionary with key "user_id" and value 42 as an integer, which FastAPI converts to JSON.
  3. Final Answer:

    {"user_id": 42} -> Option A
  4. Quick Check:

    Path param type int converts "42" to 42 [OK]
Hint: Check parameter type hints to know output data type [OK]
Common Mistakes:
  • Assuming path parameters are always strings
  • Expecting a 404 error without reason
  • Confusing JSON string with Python dict
4. What is wrong with this FastAPI path parameter code?
from fastapi import FastAPI
app = FastAPI()

@app.get("/products/{product_id}")
async def get_product(product_id):
    return {"product": product_id}
medium
A. Function must be synchronous, not async
B. Missing type annotation for product_id
C. Incorrect route path syntax
D. Return statement must be a string, not dict

Solution

  1. Step 1: Check function parameter declaration

    The parameter product_id lacks a type hint, so FastAPI treats it as a string by default but it's best practice to specify type.
  2. Step 2: Identify the issue

    While the code runs, missing type annotation can cause unexpected behavior or lack of validation.
  3. Final Answer:

    Missing type annotation for product_id -> Option B
  4. Quick Check:

    Always add type hints for path parameters [OK]
Hint: Always add type hints to path parameters for validation [OK]
Common Mistakes:
  • Ignoring type hints thinking they are optional
  • Confusing async with sync function requirements
  • Thinking route path syntax is wrong when it is correct
5. You want to create a FastAPI route that accepts two path parameters: category (string) and item_id (integer). Which of the following is the correct way to define this route and function?
hard
A. @app.get("/items/{category}/{item_id}") async def read_item(category: str, item_id: int): return {"category": category, "item_id": item_id}
B. @app.get("/items/{category}/{item_id}") async def read_item(item_id: int, category: str): return {"category": category, "item_id": item_id}
C. @app.get("/items/{category}/{item_id}") async def read_item(category, item_id): return {"category": category, "item_id": item_id}
D. @app.get("/items/{category}/{item_id}") def read_item(category: str, item_id: int): return {"category": category, "item_id": item_id}

Solution

  1. Step 1: Match path parameters with function parameters

    The route declares two path parameters in order: category then item_id. The function parameters must match this order and names.
  2. Step 2: Check type annotations and async usage

    @app.get("/items/{category}/{item_id}") async def read_item(category: str, item_id: int): return {"category": category, "item_id": item_id} correctly uses type hints (str and int) and async function, which is recommended in FastAPI.
  3. Final Answer:

    @app.get("/items/{category}/{item_id}") async def read_item(category: str, item_id: int): return {"category": category, "item_id": item_id} -> Option A
  4. Quick Check:

    Order and types must match route parameters [OK]
Hint: Order and names of parameters must match route path [OK]
Common Mistakes:
  • Swapping parameter order in function
  • Omitting type hints for path parameters
  • Using sync function without reason
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard