0
0
FastAPIframework~10 mins

Multiple query parameters in FastAPI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multiple query parameters
Client sends HTTP GET request
FastAPI receives request
Extract query parameters
Pass parameters to endpoint function
Function processes parameters
Return response with parameters info
Client receives response
The client sends a GET request with multiple query parameters. FastAPI extracts these parameters and passes them to the endpoint function, which processes and returns a response.
Execution Sample
FastAPI
from fastapi import FastAPI
from typing import List

app = FastAPI()

@app.get("/items/")
async def read_items(q: List[str] = []):
    return {"q": q}
This FastAPI endpoint accepts multiple query parameters named 'q' and returns them as a list.
Execution Table
StepRequest URLExtracted Query ParamsFunction Parameter 'q'Response
1/items/?q=apple&q=banana&q=cherryq=['apple', 'banana', 'cherry']['apple', 'banana', 'cherry']{"q": ["apple", "banana", "cherry"]}
2/items/?q=dogq=['dog']['dog']{"q": ["dog"]}
3/items/q=[] (no query)[]{"q": []}
4/items/?q=one&q=two&q=three&q=fourq=['one', 'two', 'three', 'four']['one', 'two', 'three', 'four']{"q": ["one", "two", "three", "four"]}
5Request endsNo more requestsNo more callsNo more responses
💡 No more requests to process; server waits for next client call.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
q[]['apple', 'banana', 'cherry']['dog'][]['one', 'two', 'three', 'four'][]
Key Moments - 2 Insights
Why is 'q' a list even if only one query parameter is sent?
Because the endpoint defines 'q' as List[str], FastAPI always collects all 'q' parameters into a list, even if there is only one or none, as shown in execution_table rows 1 and 2.
What happens if no 'q' parameter is provided in the URL?
The default value [] is used, so 'q' is an empty list. This is shown in execution_table row 3 where no query parameters are sent.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table row 1. What is the value of 'q' inside the function?
Aapple, banana, cherry
B['apple']
C['apple', 'banana', 'cherry']
DNone
💡 Hint
Check the 'Function Parameter q' column in row 1 of the execution_table.
At which step does the function receive an empty list for 'q'?
AStep 3
BStep 2
CStep 4
DStep 1
💡 Hint
Look for the row where 'Extracted Query Params' shows no 'q' values.
If the endpoint parameter 'q' was changed to a single string instead of a list, what would happen when multiple 'q' parameters are sent?
AFastAPI would combine all 'q' values into one string.
BFastAPI would take only the last 'q' value.
CFastAPI would raise an error.
DFastAPI would ignore all 'q' parameters.
💡 Hint
Think about how FastAPI handles single vs multiple query parameters for string vs list types.
Concept Snapshot
Multiple query parameters in FastAPI:
- Use List[type] to accept multiple values for the same query key.
- FastAPI collects all values into a list.
- If no parameters sent, default list is empty.
- Example: q: List[str] = []
- URL: /items/?q=one&q=two&q=three
- Response returns list of all q values.
Full Transcript
This example shows how FastAPI handles multiple query parameters with the same name. The endpoint defines a parameter 'q' as a list of strings. When the client sends a GET request with multiple 'q' parameters, FastAPI collects them all into a list and passes it to the function. The function then returns this list in the response. If no 'q' parameters are sent, the default empty list is used. This behavior is consistent and predictable, making it easy to handle multiple query inputs.