Challenge - 5 Problems
Boolean Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
How does FastAPI parse boolean query parameters?
Consider this FastAPI endpoint:
What will be the output if you call
from fastapi import FastAPI, Query
app = FastAPI()
@app.get("/items/")
async def read_items(active: bool = Query(False)):
return {"active": active}What will be the output if you call
/items/?active=true?FastAPI
from fastapi import FastAPI, Query app = FastAPI() @app.get("/items/") async def read_items(active: bool = Query(False)): return {"active": active}
Attempts:
2 left
💡 Hint
FastAPI automatically converts query parameters to the declared type.
✗ Incorrect
FastAPI converts the string 'true' in the query parameter to the boolean True automatically.
📝 Syntax
intermediate2:00remaining
Correct way to declare optional boolean query parameter with default
Which of the following is the correct way to declare an optional boolean query parameter named
enabled with default True in FastAPI?Attempts:
2 left
💡 Hint
Use Query to specify metadata and default values for query parameters.
✗ Incorrect
Option A correctly uses Query with default=True to declare an optional boolean query parameter with default True.
🔧 Debug
advanced2:00remaining
Why does this boolean query parameter always return False?
Given this FastAPI endpoint:
If you call
from fastapi import FastAPI
app = FastAPI()
@app.get("/check/")
async def check_flag(flag: bool = False):
return {"flag": flag}If you call
/check/?flag=0 or /check/?flag=false, the response is always {"flag": false}. But calling /check/?flag=1 returns {"flag": true}. Why does flag=0 and flag=false both return False?FastAPI
from fastapi import FastAPI app = FastAPI() @app.get("/check/") async def check_flag(flag: bool = False): return {"flag": flag}
Attempts:
2 left
💡 Hint
Check how FastAPI converts common string values to booleans.
✗ Incorrect
FastAPI converts '0', 'false', 'False', 'no', 'off' to False boolean values automatically.
❓ state_output
advanced2:00remaining
What is the output when boolean query parameter is missing?
Given this FastAPI endpoint:
What happens if you call
from fastapi import FastAPI, Query
app = FastAPI()
@app.get("/status/")
async def status(online: bool = Query(...)):
return {"online": online}What happens if you call
/status/ without the online query parameter?FastAPI
from fastapi import FastAPI, Query app = FastAPI() @app.get("/status/") async def status(online: bool = Query(...)): return {"online": online}
Attempts:
2 left
💡 Hint
Query(...) means the parameter is required.
✗ Incorrect
Using Query(...) makes the parameter required, so missing it causes a 422 error.
🧠 Conceptual
expert3:00remaining
How does FastAPI handle boolean query parameters with multiple values?
Consider this FastAPI endpoint:
What is the output of calling
from fastapi import FastAPI, Query
from typing import List
app = FastAPI()
@app.get("/flags/")
async def flags(active: List[bool] = Query([])):
return {"active": active}What is the output of calling
/flags/?active=true&active=false&active=1?FastAPI
from fastapi import FastAPI, Query from typing import List app = FastAPI() @app.get("/flags/") async def flags(active: List[bool] = Query([])): return {"active": active}
Attempts:
2 left
💡 Hint
FastAPI converts each query parameter value to the declared type in the list.
✗ Incorrect
FastAPI converts each 'active' query parameter string to boolean values in the list.