0
0
FastAPIframework~10 mins

Multiple query parameters 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 define a FastAPI GET endpoint that accepts two query parameters: 'name' and 'age'.

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/user")
async def get_user(name: str, age: [1]):
    return {"name": name, "age": age}
Drag options to blanks, or click blank then click option'
Aint
Bfloat
Cstr
Dbool
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'str' for age which should be a number.
Forgetting to specify the type for the query parameter.
2fill in blank
medium

Complete the code to make the 'age' query parameter optional with a default value of 18.

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/user")
async def get_user(name: str, age: [1] = 18):
    return {"name": name, "age": age}
Drag options to blanks, or click blank then click option'
AOptional[int]
Bstr
Cint
Dfloat
Attempts:
3 left
💡 Hint
Common Mistakes
Using Optional[int] without importing Optional.
Not assigning a default value to make the parameter optional.
3fill in blank
hard

Fix the error in the code to correctly accept multiple query parameters 'q' and 'limit'.

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/search")
async def search(q: str, limit: [1] = 10):
    return {"query": q, "limit": limit}
Drag options to blanks, or click blank then click option'
Astr
Bint
Cbool
Dfloat
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'str' for 'limit' which should be a number.
Forgetting to assign a default value to 'limit'.
4fill in blank
hard

Fill both blanks to create a FastAPI endpoint that accepts a list of tags as query parameters.

FastAPI
from fastapi import FastAPI
from typing import [1]
app = FastAPI()

@app.get("/items")
async def get_items(tags: [2][str] = []):
    return {"tags": tags}
Drag options to blanks, or click blank then click option'
AList
BDict
CSet
DTuple
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Dict' or 'Set' which are not ordered lists.
Not importing 'List' from typing.
5fill in blank
hard

Fill all three blanks to create a FastAPI endpoint that accepts multiple optional query parameters with default values.

FastAPI
from fastapi import FastAPI
from typing import [1], [2]
app = FastAPI()

@app.get("/filter")
async def filter_items(categories: [1][[2][str]] = None, limit: [3] = 5):
    return {"categories": categories, "limit": limit}
Drag options to blanks, or click blank then click option'
AOptional
BList
Cint
DDict
Attempts:
3 left
💡 Hint
Common Mistakes
Not importing Optional when using it.
Using Dict instead of List for categories.
Not assigning a default value to limit.