Complete the code to declare a boolean query parameter in FastAPI.
from fastapi import FastAPI, Query app = FastAPI() @app.get("/items/") async def read_items(available: bool = Query([1])): return {"available": available}
The default value for a boolean query parameter should be a boolean type like False. Using False sets the default to false.
Complete the code to make the boolean query parameter optional with a default of None.
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}
To make a boolean query parameter optional, set its default to None and use Optional[bool] as the type.
Fix the error in the code by completing the boolean query parameter declaration correctly.
from fastapi import FastAPI, Query app = FastAPI() @app.get("/items/") async def read_items(available: bool = [1]): return {"available": available}
The default value inside Query() for a boolean parameter must be a boolean, like True. Using strings causes errors.
Fill both blanks to declare an optional boolean query parameter with a default of None and a description.
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}
Use None as the default to make the parameter optional. The description should be a string explaining the parameter.
Fill all three blanks to declare a boolean query parameter with a default, alias, and description.
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}
The default is False. The alias is a string used in the query URL. The description explains the parameter.