Challenge - 5 Problems
FastAPI Default Values Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when calling the endpoint without query parameters?
Consider this FastAPI endpoint using default values for query parameters. What will the response be if you call
/items/42 without any query parameters?FastAPI
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Item(BaseModel): name: str description: str = "No description provided" @app.get("/items/{item_id}") async def read_item(item_id: int, q: str = "default_query"): return {"item_id": item_id, "q": q}
Attempts:
2 left
💡 Hint
Think about what happens when a query parameter has a default value and is not provided by the client.
✗ Incorrect
The query parameter 'q' has a default value of 'default_query'. If the client does not provide 'q', FastAPI uses the default. So the response includes 'q' with the default string.
📝 Syntax
intermediate2:00remaining
Which option correctly sets a default value for a path parameter in FastAPI?
FastAPI path parameters cannot have default values. Which of the following code snippets correctly defines a path parameter and a query parameter with a default value?
FastAPI
from fastapi import FastAPI app = FastAPI() # Choose the correct snippet:
Attempts:
2 left
💡 Hint
Path parameters must always be provided in the URL path. Query parameters can have defaults.
✗ Incorrect
Path parameters cannot have default values because they are part of the URL path and must be specified. Query parameters can have defaults. Option C correctly defines a required path parameter and a query parameter with a default.
❓ state_output
advanced2:00remaining
What is the response when a request omits an optional query parameter with a default value?
Given this FastAPI endpoint, what will be the JSON response if the client calls
/search without any query parameters?FastAPI
from fastapi import FastAPI app = FastAPI() @app.get("/search") async def search_items(term: str = "all", limit: int = 10): return {"term": term, "limit": limit}
Attempts:
2 left
💡 Hint
Check what happens when query parameters have default values and are not provided.
✗ Incorrect
Both 'term' and 'limit' have default values. If the client omits them, FastAPI uses the defaults. So the response includes 'term' as 'all' and 'limit' as 10.
🔧 Debug
advanced2:00remaining
Why does this FastAPI endpoint raise a validation error?
Examine this FastAPI endpoint. Why does calling
/products without query parameters cause a 422 error?FastAPI
from fastapi import FastAPI app = FastAPI() @app.get("/products") async def list_products(category: str): return {"category": category}
Attempts:
2 left
💡 Hint
Think about which parameters FastAPI expects from the client and which have defaults.
✗ Incorrect
The 'category' parameter is required but has no default. If the client omits it, FastAPI returns a 422 error because it cannot validate the missing required parameter.
🧠 Conceptual
expert3:00remaining
How does FastAPI handle default values for mutable types in query parameters?
Consider this FastAPI endpoint with a query parameter defaulting to an empty list. What is the correct way to define a default empty list for a query parameter to avoid unexpected behavior?
FastAPI
from fastapi import FastAPI, Query app = FastAPI() @app.get("/tags") async def get_tags(tags: list[str] = []): return {"tags": tags}
Attempts:
2 left
💡 Hint
Mutable default arguments can cause shared state issues in Python functions.
✗ Incorrect
Using a mutable default argument like [] directly can cause all calls to share the same list, leading to bugs. The recommended FastAPI way is to use Query(default_factory=list) to create a new list for each request.