0
0
FastAPIframework~20 mins

Why query parameters filter data in FastAPI - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Query Parameter Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why do query parameters filter data in FastAPI?

In FastAPI, query parameters are often used to filter data. Why is this approach useful?

ABecause query parameters allow clients to specify exactly which data they want, reducing unnecessary data transfer.
BBecause query parameters automatically cache the data on the server for faster responses.
CBecause query parameters encrypt the data sent from the client to the server for security.
DBecause query parameters force the server to send all data regardless of client needs.
Attempts:
2 left
💡 Hint

Think about how filtering helps reduce the amount of data sent over the network.

component_behavior
intermediate
2:00remaining
What output does this FastAPI endpoint produce with query filtering?

Given this FastAPI endpoint, what is the output when called with ?category=books?

FastAPI
from fastapi import FastAPI, Query
app = FastAPI()

items = [
    {"id": 1, "category": "books"},
    {"id": 2, "category": "electronics"},
    {"id": 3, "category": "books"}
]

@app.get("/items")
def read_items(category: str | None = Query(None)):
    if category:
        return [item for item in items if item["category"] == category]
    return items
A[{"id": 1, "category": "books"}, {"id": 2, "category": "electronics"}, {"id": 3, "category": "books"}]
B[{"id": 2, "category": "electronics"}]
C[]
D[{"id": 1, "category": "books"}, {"id": 3, "category": "books"}]
Attempts:
2 left
💡 Hint

Look at how the list comprehension filters items by category.

📝 Syntax
advanced
2:00remaining
Which option correctly defines an optional query parameter in FastAPI?

Which code snippet correctly defines an optional query parameter named status with a default value of None?

Adef get_items(status: Optional[str] = Query(None)):
Bdef get_items(status: str | None = Query(None)):
Cdef get_items(status: str = None):
Ddef get_items(status: str = Query()):
Attempts:
2 left
💡 Hint

Consider modern Python typing and FastAPI's Query usage.

🔧 Debug
advanced
2:00remaining
Why does this FastAPI query parameter filter not work as expected?

Consider this code snippet. Why does filtering by color not work?

from fastapi import FastAPI
app = FastAPI()

items = [{"id": 1, "color": "red"}, {"id": 2, "color": "blue"}]

@app.get("/items")
def read_items(color: str):
    return [item for item in items if item["color"] == color]
ABecause the 'color' key does not exist in the items dictionary.
BBecause the list comprehension syntax is invalid and causes a runtime error.
CBecause the query parameter 'color' is required but no default is set, causing a validation error if missing.
DBecause FastAPI does not support filtering with query parameters.
Attempts:
2 left
💡 Hint

Think about what happens if the client does not provide the 'color' query parameter.

state_output
expert
3:00remaining
What is the value of 'filtered' after this FastAPI endpoint runs with '?min_price=50'?

Analyze the code and determine the value of the variable filtered after calling the endpoint with query parameter min_price=50.

FastAPI
from fastapi import FastAPI, Query
app = FastAPI()

products = [
    {"id": 1, "price": 30},
    {"id": 2, "price": 50},
    {"id": 3, "price": 70}
]

@app.get("/products")
def get_products(min_price: int = Query(0)):
    filtered = [p for p in products if p["price"] >= min_price]
    return filtered
A[{"id": 2, "price": 50}, {"id": 3, "price": 70}]
B[{"id": 1, "price": 30}, {"id": 2, "price": 50}, {"id": 3, "price": 70}]
C[{"id": 3, "price": 70}]
D[]
Attempts:
2 left
💡 Hint

Check which products have price greater than or equal to 50.