0
0
FastAPIframework~10 mins

Default values 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 set a default value for the query parameter.

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'
ANone
B"default"
C0
DTrue
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to use quotes for string default values.
Using None without typing Optional[str].
2fill in blank
medium

Complete the code to set a default integer value for the path parameter.

FastAPI
from fastapi import FastAPI

app = FastAPI()

@app.get("/users/{user_id}")
async def read_user(user_id: int = [1]):
    return {"user_id": user_id}
Drag options to blanks, or click blank then click option'
ANone
B"1"
C1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using string values for integer parameters.
Setting None as default for non-optional parameters.
3fill in blank
hard

Fix the error in setting a default value for an optional query parameter.

FastAPI
from typing import Optional
from fastapi import FastAPI

app = FastAPI()

@app.get("/search/")
async def search(q: Optional[str] = [1]):
    return {"q": q}
Drag options to blanks, or click blank then click option'
ATrue
B""
C0
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Using empty string instead of None for optional parameters.
Not importing Optional from typing.
4fill in blank
hard

Fill both blanks to set a default value and type for a query parameter.

FastAPI
from fastapi import FastAPI

app = FastAPI()

@app.get("/products/")
async def get_products(limit: [1] = [2]):
    return {"limit": limit}
Drag options to blanks, or click blank then click option'
Aint
B10
Cstr
D"10"
Attempts:
3 left
💡 Hint
Common Mistakes
Using string default value for an integer type.
Swapping type and default value positions.
5fill in blank
hard

Fill all three blanks to create a default value for a query parameter with a description.

FastAPI
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}
Drag options to blanks, or click blank then click option'
A"default"
B"Search query string"
C3
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Putting None as default when a string default is expected.
Using quotes incorrectly around integers.