0
0
FastAPIframework~10 mins

Boolean query parameters in FastAPI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a boolean query parameter in FastAPI.

FastAPI
from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/items/")
async def read_items(available: bool = Query([1])):
    return {"available": available}
Drag options to blanks, or click blank then click option'
ATrue
BNone
C"true"
DFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using string values like "true" or "false" instead of boolean True or False.
Leaving the default as None which is not a boolean.
2fill in blank
medium

Complete the code to make the boolean query parameter optional with a default of None.

FastAPI
from typing import Optional
from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/items/")
async def read_items(available: Optional[bool] = Query([1])):
    return {"available": available}
Drag options to blanks, or click blank then click option'
A"false"
BFalse
CNone
DTrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using a boolean default like True or False when the parameter should be optional.
Using string values instead of None for optional parameters.
3fill in blank
hard

Fix the error in the code by completing the boolean query parameter declaration correctly.

FastAPI
from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/items/")
async def read_items(available: bool = [1]):
    return {"available": available}
Drag options to blanks, or click blank then click option'
AQuery(True)
BQuery("False")
CQuery("true")
DQuery(None)
Attempts:
3 left
💡 Hint
Common Mistakes
Passing string values like "true" or "False" inside Query for boolean parameters.
Passing None without Optional type hint.
4fill in blank
hard

Fill both blanks to declare an optional boolean query parameter with a default of None and a description.

FastAPI
from typing import Optional
from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/items/")
async def read_items(available: Optional[bool] = Query([1], description=[2])):
    return {"available": available}
Drag options to blanks, or click blank then click option'
ANone
B"Is the item available?"
CTrue
D"Optional boolean parameter"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a boolean default instead of None for optional parameters.
Passing non-string values as description.
5fill in blank
hard

Fill all three blanks to declare a boolean query parameter with a default, alias, and description.

FastAPI
from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/items/")
async def read_items(
    available: bool = Query(
        default=[1],
        alias=[2],
        description=[3]
    )
):
    return {"available": available}
Drag options to blanks, or click blank then click option'
AFalse
B"is_available"
C"Filter items by availability"
DTrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-boolean default values.
Using non-string alias or description.