0
0
FastAPIframework~10 mins

Basic query parameter declaration 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 query parameter named 'q' in a FastAPI path operation.

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/items/")
async def read_items(q: str = [1]):
    return {"q": q}
Drag options to blanks, or click blank then click option'
A"default"
BTrue
CNone
D...
Attempts:
3 left
💡 Hint
Common Mistakes
Using '...' without importing it
Setting default to True which is not a string or None
Forgetting to set a default value
2fill in blank
medium

Complete the code to declare an integer query parameter named 'page' with default 1.

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/users/")
async def list_users(page: [1] = 1):
    return {"page": page}
Drag options to blanks, or click blank then click option'
Aint
Bstr
Cfloat
Dbool
Attempts:
3 left
💡 Hint
Common Mistakes
Using string type for numeric parameters
Forgetting to set a default value
Using float instead of int
3fill in blank
hard

Fix the error in the code by completing the query parameter declaration for 'limit' as an optional integer.

FastAPI
from typing import Optional
from fastapi import FastAPI
app = FastAPI()

@app.get("/products/")
async def get_products(limit: [1] = None):
    return {"limit": limit}
Drag options to blanks, or click blank then click option'
Aint
BOptional[str]
Cstr
DOptional[int]
Attempts:
3 left
💡 Hint
Common Mistakes
Using just int without Optional when default is None
Using Optional[str] when expecting int
Not importing Optional
4fill in blank
hard

Fill both blanks to declare a query parameter 'search' as a string with default empty string and 'limit' as an integer with default 10.

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/items/")
async def search_items(search: [1] = "", limit: [2] = 10):
    return {"search": search, "limit": limit}
Drag options to blanks, or click blank then click option'
Astr
Bint
Cfloat
Dbool
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing types between parameters
Using float or bool incorrectly
Not matching default values to types
5fill in blank
hard

Fill all three blanks to declare query parameters: 'q' as optional string with default None, 'page' as int default 1, and 'size' as int default 20.

FastAPI
from typing import Optional
from fastapi import FastAPI
app = FastAPI()

@app.get("/search/")
async def search(q: [1] = None, page: [2] = 1, size: [3] = 20):
    return {"q": q, "page": page, "size": size}
Drag options to blanks, or click blank then click option'
AOptional[str]
Bint
Cstr
DOptional[int]
Attempts:
3 left
💡 Hint
Common Mistakes
Using str instead of Optional[str] for optional parameters
Using Optional[int] unnecessarily for page and size
Mixing types between parameters