Complete the code to set a default value for the query parameter.
from fastapi import FastAPI app = FastAPI() @app.get("/items/") async def read_items(q: str = [1]): return {"q": q}
The default value for the query parameter q is set by assigning it in the function signature. Here, "default" is the default string value.
Complete the code to set a default integer value for the path parameter.
from fastapi import FastAPI app = FastAPI() @app.get("/users/{user_id}") async def read_user(user_id: int = [1]): return {"user_id": user_id}
The default value for the path parameter user_id must be an integer. Here, 1 is the correct default integer value without quotes.
Fix the error in setting a default value for an optional query parameter.
from typing import Optional from fastapi import FastAPI app = FastAPI() @app.get("/search/") async def search(q: Optional[str] = [1]): return {"q": q}
When using Optional[str], the default value should be None to indicate the parameter is optional and may be omitted.
Fill both blanks to set a default value and type for a query parameter.
from fastapi import FastAPI app = FastAPI() @app.get("/products/") async def get_products(limit: [1] = [2]): return {"limit": limit}
The query parameter limit is typed as int and has a default value of 10 (an integer).
Fill all three blanks to create a default value for a query parameter with a description.
from fastapi import FastAPI, Query app = FastAPI() @app.get("/items/") async def read_items(q: str = Query([1], description=[2], min_length=[3])): return {"q": q}
The query parameter q uses Query to set a default value "default", a description, and a minimum length of 3 characters.