Query parameters are parts of the URL that come after a question mark (?) and are used to send extra information to the server. They help specify what data the client wants.
Click to reveal answer
beginner
How do query parameters help filter data in FastAPI?
They let users tell the server exactly what data to return by adding conditions in the URL, like filtering a list to show only items that match those conditions.
Click to reveal answer
beginner
Example: How to define an optional query parameter in FastAPI?
Use function parameters with default values. For example, def read_items(q: str = None): means the query parameter 'q' is optional.
Click to reveal answer
beginner
Why is filtering data with query parameters better than sending all data?
Filtering reduces the amount of data sent over the network, making responses faster and easier to handle, just like choosing only the fruits you want from a basket.
Click to reveal answer
beginner
What happens if you don't use query parameters to filter data?
The server sends all data, which can be slow and overwhelming. Query parameters help avoid this by letting you ask for just what you need.
Click to reveal answer
What symbol starts query parameters in a URL?
A&
B#
C/
D?
✗ Incorrect
Query parameters always start after a question mark (?) in a URL.
In FastAPI, how do you make a query parameter optional?
ASet a default value in the function parameter
BUse a special decorator
CMake the parameter a global variable
DUse a POST request instead
✗ Incorrect
Setting a default value like None makes the query parameter optional.
Why use query parameters to filter data?
ATo make the URL shorter
BTo get only the data you want
CTo change the server code
DTo send data to the client
✗ Incorrect
Query parameters help you ask for specific data, filtering results.
Which of these is a valid query parameter example?
A/items/color/red/size/large
B/items#color=red
C/items?color=red&size=large
D/items/color=red&size=large
✗ Incorrect
Query parameters come after ? and are separated by &.
What happens if you omit query parameters when calling a FastAPI endpoint?
AThe endpoint uses default values or returns all data
BThe server crashes
CThe client gets an error message
DThe URL changes automatically
✗ Incorrect
If query parameters are optional, the endpoint uses defaults or returns unfiltered data.
Explain in your own words why query parameters are useful for filtering data in FastAPI.
Think about how you ask for specific items in a store.
You got /4 concepts.
Describe how to define and use an optional query parameter in a FastAPI function.
Look at function parameters with = None.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of query parameters in a FastAPI endpoint?
easy
A. To filter or customize the data returned by the API
B. To define the HTTP method used by the endpoint
C. To specify the database connection details
D. To set the server port number
Solution
Step 1: Understand what query parameters do
Query parameters are used to send extra information in the URL to control what data the API returns.
Step 2: Identify the purpose in FastAPI
In FastAPI, query parameters let users filter or customize the response without changing the endpoint itself.
Final Answer:
To filter or customize the data returned by the API -> Option A
Quick Check:
Query parameters = filter/customize data [OK]
Hint: Query parameters control what data you get back [OK]
Common Mistakes:
Confusing query parameters with HTTP methods
Thinking query parameters set server or database settings
Assuming query parameters change endpoint URLs
2. Which of the following is the correct way to declare an optional query parameter named category in FastAPI?
easy
A. def read_items(category: str):
B. def read_items(category: str = None):
C. def read_items(category):
D. def read_items(category: int = 0):
Solution
Step 1: Recall how to declare optional query parameters
In FastAPI, optional query parameters are declared by giving a default value, often None.
Step 2: Check the options for correct syntax
def read_items(category: str = None): uses category: str = None, which means the parameter is optional and of type string.
Final Answer:
def read_items(category: str = None): -> Option B
Quick Check:
Optional query param = default None [OK]
Hint: Optional query params have default values like None [OK]
Common Mistakes:
Not providing a default value for optional parameters
Using wrong data types for query parameters
Omitting type annotations
3. Given this FastAPI endpoint:
from fastapi import FastAPI
app = FastAPI()
items = [
{"name": "apple", "category": "fruit"},
{"name": "carrot", "category": "vegetable"}
]
@app.get("/items")
async def read_items(category: str = None):
if category:
return [item for item in items if item["category"] == category]
return items
What will be the output of a GET request to /items?category=fruit?
medium
A. [{"name": "apple", "category": "fruit"}]
B. [{"name": "carrot", "category": "vegetable"}]
C. []
D. [{"name": "apple", "category": "fruit"}, {"name": "carrot", "category": "vegetable"}]
Solution
Step 1: Understand the query parameter usage
The endpoint checks if category is given, then filters items matching that category.
Step 2: Apply the filter for category='fruit'
Only the item with category 'fruit' is the apple, so the returned list contains only that item.
Final Answer:
[{"name": "apple", "category": "fruit"}] -> Option A
Quick Check:
Filter by category 'fruit' returns apple [OK]
Hint: Filter returns only items matching query parameter value [OK]
Common Mistakes:
Returning all items ignoring the filter
Returning empty list when filter matches
Confusing category values
4. Identify the error in this FastAPI endpoint that tries to filter items by a query parameter color:
from fastapi import FastAPI
app = FastAPI()
items = [
{"name": "shirt", "color": "blue"},
{"name": "pants", "color": "black"}
]
@app.get("/items")
def read_items(color):
return [item for item in items if item["color"] == color]
medium
A. The items list is empty
B. The list comprehension syntax is incorrect
C. The endpoint should use POST instead of GET
D. The query parameter 'color' is missing a type annotation and default value
Solution
Step 1: Check function parameter declaration
The parameter 'color' lacks a type annotation and default value, so FastAPI treats it as a required path parameter, causing errors.
Step 2: Correct declaration for query parameter
Adding a type (e.g., str) and a default (e.g., None) makes it an optional query parameter, fixing the error.
Final Answer:
The query parameter 'color' is missing a type annotation and default value -> Option D
Quick Check:
Query params need type and default [OK]
Hint: Query params must have type and default to work [OK]
Common Mistakes:
Omitting type annotations for query parameters
Confusing query parameters with path parameters
Using wrong HTTP methods for filtering
5. You want to create a FastAPI endpoint that returns products filtered by optional query parameters category and max_price. Which code snippet correctly implements this filtering?
hard
A. async def get_products(category: str, max_price: float):
return products
B. async def get_products(category, max_price):
return [p for p in products if p['category'] == category and p['price'] <= max_price]
C. async def get_products(category: str = None, max_price: float = None):
filtered = products
if category:
filtered = [p for p in filtered if p['category'] == category]
if max_price is not None:
filtered = [p for p in filtered if p['price'] <= max_price]
return filtered
Both category and max_price are optional, so they need default values (None) to allow filtering only when provided.
Step 2: Check filtering logic
async def get_products(category: str = None, max_price: float = None):
filtered = products
if category:
filtered = [p for p in filtered if p['category'] == category]
if max_price is not None:
filtered = [p for p in filtered if p['price'] <= max_price]
return filtered filters products step-by-step, applying each filter only if the parameter is given, which is correct and safe.
Final Answer:
async def get_products(category: str = None, max_price: float = None):
filtered = products
if category:
filtered = [p for p in filtered if p['category'] == category]
if max_price is not None:
filtered = [p for p in filtered if p['price'] <= max_price]
return filtered -> Option C
Quick Check:
Optional filters with defaults and conditions = correct [OK]
Hint: Use defaults None and conditionally filter inside function [OK]
Common Mistakes:
Not providing default values for optional parameters
Filtering without checking if parameters exist
Returning unfiltered data ignoring query parameters
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.