0
0
FastAPIframework~10 mins

Query parameter validation 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' as optional.

FastAPI
from fastapi import FastAPI
from typing import *

app = FastAPI()

@app.get("/items/")
async def read_items(q: [1] = None):
    return {"q": q}
Drag options to blanks, or click blank then click option'
AList[str]
BOptional[str]
Cint
Dstr
Attempts:
3 left
💡 Hint
Common Mistakes
Using just 'str' makes the parameter required.
Using 'int' when the parameter should be a string.
2fill in blank
medium

Complete the code to set a minimum length of 3 for the query parameter 'q'.

FastAPI
from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/items/")
async def read_items(q: str = Query([1], min_length=3)):
    return {"q": q}
Drag options to blanks, or click blank then click option'
A''
BNone
C...
D""
Attempts:
3 left
💡 Hint
Common Mistakes
Using None makes the parameter optional.
Using empty strings does not enforce required parameter.
3fill in blank
hard

Fix the error in the code to correctly validate that 'q' is an integer query parameter with a maximum value of 10.

FastAPI
from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/items/")
async def read_items(q: int = Query([1], le=10)):
    return {"q": q}
Drag options to blanks, or click blank then click option'
A0
B10
CNone
D...
Attempts:
3 left
💡 Hint
Common Mistakes
Using None makes the parameter optional.
Using a default value disables required validation.
4fill in blank
hard

Fill both blanks to declare a query parameter 'q' that is optional and has a regex pattern to allow only letters.

FastAPI
from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/items/")
async def read_items(q: str = Query([1], regex=[2])):
    return {"q": q}
Drag options to blanks, or click blank then click option'
ANone
B"^[a-zA-Z]+$"
C"^\d+$"
D...
Attempts:
3 left
💡 Hint
Common Mistakes
Using Ellipsis makes the parameter required.
Using wrong regex pattern allows digits.
5fill in blank
hard

Fill all three blanks to declare a query parameter 'q' that is required, has a minimum length of 2, and a maximum length of 5.

FastAPI
from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/items/")
async def read_items(q: str = Query([1], min_length=[2], max_length=[3])):
    return {"q": q}
Drag options to blanks, or click blank then click option'
ANone
B2
C5
D...
Attempts:
3 left
💡 Hint
Common Mistakes
Using None makes the parameter optional.
Swapping min_length and max_length values.