0
0
FastAPIframework~10 mins

Optional 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 declare an optional query parameter with a default value.

FastAPI
from fastapi import FastAPI
from typing import [1]

app = FastAPI()

@app.get("/items/")
async def read_items(q: [1][str] = None):
    return {"q": q}
Drag options to blanks, or click blank then click option'
AList
BOptional
CDict
DSet
Attempts:
3 left
💡 Hint
Common Mistakes
Using List or Dict instead of Optional for optional parameters.
Not importing Optional from typing.
2fill in blank
medium

Complete the code to import the correct function to declare an optional query parameter with a default value.

FastAPI
from fastapi import FastAPI, [1]

app = FastAPI()

@app.get("/users/")
async def get_users(name: str = [1](None)):
    return {"name": name}
Drag options to blanks, or click blank then click option'
AQuery
BDepends
CPath
DBody
Attempts:
3 left
💡 Hint
Common Mistakes
Using Depends or Body instead of Query for query parameters.
Not importing Query from fastapi.
3fill in blank
hard

Fix the error in the code by completing the query parameter declaration to make it optional with a default value.

FastAPI
from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/search/")
async def search_items(q: str = [1]):
    return {"q": q}
Drag options to blanks, or click blank then click option'
AQuery(default=None)
BQuery()
CQuery(None)
DQuery(default="")
Attempts:
3 left
💡 Hint
Common Mistakes
Using Query() without arguments makes the parameter required.
Using default empty string instead of None.
4fill in blank
hard

Fill both blanks to declare an optional integer query parameter with a default value of 10.

FastAPI
from fastapi import FastAPI, [1]

app = FastAPI()

@app.get("/numbers/")
async def get_number(limit: int = [1]([2])):
    return {"limit": limit}
Drag options to blanks, or click blank then click option'
AQuery
BDepends
C10
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Using Depends instead of Query.
Using None as default instead of 10.
5fill in blank
hard

Fill all three blanks to declare an optional string query parameter with a default value and a description.

FastAPI
from fastapi import FastAPI, [1]

app = FastAPI()

@app.get("/products/")
async def list_products(q: str = [2]):
    return {"query": q}

# Use Query with default value "book" and description [3]
Drag options to blanks, or click blank then click option'
AQuery
BQuery("book", description="Search term")
C"Search term"
DQuery(default="book", description="Search term")
Attempts:
3 left
💡 Hint
Common Mistakes
Putting description as a separate string instead of inside Query.
Not using named arguments for default and description.