Challenge - 5 Problems
FastAPI Optional Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when no query parameter is provided?
Consider this FastAPI endpoint that uses an optional query parameter with a default value. What will be the response if the client calls the endpoint without any query parameters?
FastAPI
from fastapi import FastAPI from typing import Optional app = FastAPI() @app.get('/items/') async def read_items(q: Optional[str] = None): if q: return {'query': q} return {'query': 'No query provided'}
Attempts:
2 left
💡 Hint
Think about what happens when the optional parameter is not sent by the client.
✗ Incorrect
The parameter 'q' is optional and defaults to None. When no query parameter is sent, 'q' is None, so the function returns {'query': 'No query provided'}.
📝 Syntax
intermediate2:00remaining
Which option correctly declares an optional query parameter with a default value?
You want to declare an optional query parameter named 'page' that defaults to 1. Which of the following FastAPI endpoint parameter declarations is correct?
Attempts:
2 left
💡 Hint
Remember how to set default values for parameters in Python functions.
✗ Incorrect
Declaring 'page: int = 1' makes 'page' optional with a default value of 1. Using Optional[int] without a default value makes it required.
❓ state_output
advanced2:00remaining
What is the response when multiple optional query parameters are missing?
Given this FastAPI endpoint with two optional query parameters, what will be the JSON response if the client calls the endpoint without any query parameters?
FastAPI
from fastapi import FastAPI from typing import Optional app = FastAPI() @app.get('/search/') async def search(q: Optional[str] = None, limit: Optional[int] = 10): return {'q': q, 'limit': limit}
Attempts:
2 left
💡 Hint
Check the default values assigned to the parameters.
✗ Incorrect
Parameter 'q' defaults to None (null in JSON), and 'limit' defaults to 10. So the response includes these values.
🔧 Debug
advanced2:00remaining
Why does this FastAPI endpoint raise a validation error?
Examine the following FastAPI endpoint. Why does it raise a 422 validation error when called without query parameters?
FastAPI
from fastapi import FastAPI from typing import Optional app = FastAPI() @app.get('/items/') async def read_items(q: Optional[str]): return {'query': q}
Attempts:
2 left
💡 Hint
Think about how Python treats parameters without default values.
✗ Incorrect
In Python, parameters without default values are required. 'q: Optional[str]' without '= None' means 'q' is required, so calling without it causes validation error.
🧠 Conceptual
expert2:00remaining
How does FastAPI handle optional query parameters with default values in OpenAPI docs?
When you declare an optional query parameter with a default value in FastAPI, how does it appear in the generated OpenAPI documentation?
Attempts:
2 left
💡 Hint
Think about how default values affect API documentation.
✗ Incorrect
FastAPI uses the default value to mark the parameter as optional in OpenAPI docs and shows the default value to API users.