0
0
FastAPIframework~10 mins

Required 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 a required query parameter named 'item_id'.

FastAPI
from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/items/")
async def read_item(item_id: str = [1]):
    return {"item_id": item_id}
Drag options to blanks, or click blank then click option'
AQuery(...)
BQuery(None)
COptional[str]
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Using Query(None) makes the parameter optional.
Setting default to None makes it optional.
2fill in blank
medium

Complete the code to declare a required integer query parameter named 'page'.

FastAPI
from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/users/")
async def get_users(page: int = [1]):
    return {"page": page}
Drag options to blanks, or click blank then click option'
AQuery(...)
BQuery(1)
CQuery(None)
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Using Query(1) sets a default, so parameter is optional.
Using None makes it optional.
3fill in blank
hard

Fix the error in the code to make 'q' a required query parameter.

FastAPI
from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/search/")
async def search(q: str = [1]):
    return {"query": q}
Drag options to blanks, or click blank then click option'
AQuery(None)
BNone
CQuery(...)
D""
Attempts:
3 left
💡 Hint
Common Mistakes
Using Query(None) makes parameter optional.
Using None as default makes it optional.
4fill in blank
hard

Fill both blanks to declare two required query parameters: 'name' (str) and 'age' (int).

FastAPI
from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/users/")
async def get_user(name: str = [1], age: int = [2]):
    return {"name": name, "age": age}
Drag options to blanks, or click blank then click option'
AQuery(...)
BQuery(None)
CNone
D""
Attempts:
3 left
💡 Hint
Common Mistakes
Using Query(None) or None makes parameters optional.
5fill in blank
hard

Fill all three blanks to declare required query parameters 'q' (str), 'limit' (int), and 'offset' (int).

FastAPI
from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/items/")
async def read_items(q: str = [1], limit: int = [2], offset: int = [3]):
    return {"q": q, "limit": limit, "offset": offset}
Drag options to blanks, or click blank then click option'
AQuery(None)
BQuery(...)
CNone
D""
Attempts:
3 left
💡 Hint
Common Mistakes
Using Query(None) or None makes parameters optional.