Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a required query parameter named 'item_id'.
FastAPI
from fastapi import FastAPI, Query app = FastAPI() @app.get("/items/") async def read_item(item_id: str = [1]): return {"item_id": item_id}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Query(None) makes the parameter optional.
Setting default to None makes it optional.
✗ Incorrect
Using Query(...) makes the query parameter required in FastAPI.
2fill in blank
mediumComplete the code to declare a required integer query parameter named 'page'.
FastAPI
from fastapi import FastAPI, Query app = FastAPI() @app.get("/users/") async def get_users(page: int = [1]): return {"page": page}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Query(1) sets a default, so parameter is optional.
Using None makes it optional.
✗ Incorrect
Using Query(...) makes the 'page' parameter required, regardless of type.
3fill in blank
hardFix the error in the code to make 'q' a required query parameter.
FastAPI
from fastapi import FastAPI, Query app = FastAPI() @app.get("/search/") async def search(q: str = [1]): return {"query": q}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Query(None) makes parameter optional.
Using None as default makes it optional.
✗ Incorrect
To make 'q' required, use Query(...). Other defaults make it optional.
4fill in blank
hardFill both blanks to declare two required query parameters: 'name' (str) and 'age' (int).
FastAPI
from fastapi import FastAPI, Query app = FastAPI() @app.get("/users/") async def get_user(name: str = [1], age: int = [2]): return {"name": name, "age": age}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Query(None) or None makes parameters optional.
✗ Incorrect
Both parameters use Query(...) to be required.
5fill in blank
hardFill all three blanks to declare required query parameters 'q' (str), 'limit' (int), and 'offset' (int).
FastAPI
from fastapi import FastAPI, Query app = FastAPI() @app.get("/items/") async def read_items(q: str = [1], limit: int = [2], offset: int = [3]): return {"q": q, "limit": limit, "offset": offset}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Query(None) or None makes parameters optional.
✗ Incorrect
All three parameters use Query(...) to be required query parameters.