0
0
FastAPIframework~20 mins

Boolean query parameters in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Boolean Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does FastAPI parse boolean query parameters?
Consider this FastAPI endpoint:
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}
A{"active": "true"}
B{"active": true}
C{"active": false}
DHTTP 422 Unprocessable Entity error
Attempts:
2 left
💡 Hint
FastAPI automatically converts query parameters to the declared type.
📝 Syntax
intermediate
2: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?
Aasync def endpoint(enabled: bool = Query(default=True)):
Basync def endpoint(enabled: Optional[bool] = Query(True)):
Casync def endpoint(enabled: bool = True):
Dasync def endpoint(enabled: bool = Query(True, alias='enabled')):
Attempts:
2 left
💡 Hint
Use Query to specify metadata and default values for query parameters.
🔧 Debug
advanced
2:00remaining
Why does this boolean query parameter always return False?
Given this FastAPI endpoint:
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}
AFastAPI raises an error for '0' but not for 'false'.
BFastAPI treats any string as True except empty strings.
CFastAPI treats '0' and 'false' as False boolean values by design.
DFastAPI ignores query parameters named 'flag'.
Attempts:
2 left
💡 Hint
Check how FastAPI converts common string values to booleans.
state_output
advanced
2:00remaining
What is the output when boolean query parameter is missing?
Given this FastAPI endpoint:
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}
AReturns HTTP 422 Unprocessable Entity error because parameter is required
BReturns {"online": false} because default is False
CReturns {"online": true} because default is True
DReturns {"online": null} because parameter is missing
Attempts:
2 left
💡 Hint
Query(...) means the parameter is required.
🧠 Conceptual
expert
3:00remaining
How does FastAPI handle boolean query parameters with multiple values?
Consider this FastAPI endpoint:
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}
A{"active": [true]}
B{"active": ["true", "false", "1"]}
CHTTP 422 Unprocessable Entity error
D{"active": [true, false, true]}
Attempts:
2 left
💡 Hint
FastAPI converts each query parameter value to the declared type in the list.