Complete the code to declare an optional query parameter with a default value.
from fastapi import FastAPI from typing import [1] app = FastAPI() @app.get("/items/") async def read_items(q: [1][str] = None): return {"q": q}
The Optional type from typing allows declaring a query parameter that can be None if not provided.
Complete the code to import the correct function to declare an optional query parameter with a default value.
from fastapi import FastAPI, [1] app = FastAPI() @app.get("/users/") async def get_users(name: str = [1](None)): return {"name": name}
The Query function from FastAPI is used to declare and customize query parameters, including setting default values.
Fix the error in the code by completing the query parameter declaration to make it optional with a default value.
from fastapi import FastAPI, Query app = FastAPI() @app.get("/search/") async def search_items(q: str = [1]): return {"q": q}
Using Query(None) declares the query parameter as optional with a default value of None.
Fill both blanks to declare an optional integer query parameter with a default value of 10.
from fastapi import FastAPI, [1] app = FastAPI() @app.get("/numbers/") async def get_number(limit: int = [1]([2])): return {"limit": limit}
Use Query to declare the query parameter and set its default value to 10.
Fill all three blanks to declare an optional string query parameter with a default value and a description.
from fastapi import FastAPI, [1] app = FastAPI() @app.get("/products/") async def list_products(q: str = [2]): return {"query": q} # Use Query with default value "book" and description [3]
Import Query, then use it with default value and description to declare the optional query parameter.