Consider this FastAPI endpoint that validates a query parameter q to be a string with a minimum length of 5 and maximum length of 10:
from fastapi import FastAPI, Query
app = FastAPI()
@app.get("/items/")
async def read_items(q: str = Query(..., min_length=5, max_length=10)):
return {"q": q}What will be the response when a client requests /items/?q=fastapi123?
from fastapi import FastAPI, Query app = FastAPI() @app.get("/items/") async def read_items(q: str = Query(..., min_length=5, max_length=10)): return {"q": q}
Check the length of the string fastapi123 and compare it with the min and max length constraints.
The string fastapi123 has 10 characters, which is within the allowed range of 5 to 10 characters. So the endpoint returns the query parameter as JSON.
Choose the correct FastAPI query parameter declaration for an optional integer count with default 10, minimum 1, and maximum 100.
Remember that ge and le are used for numeric bounds, and min_length and max_length are for strings.
Option B correctly uses ge and le for numeric bounds and sets default to 10. Option B uses wrong parameter names and Optional without import. Options A and D misuse string length parameters.
Given this FastAPI endpoint:
from fastapi import FastAPI, Query
from typing import List
app = FastAPI()
@app.get("/search/")
async def search(tags: List[str] = Query([])):
return {"tags": tags}What will be the JSON response when the client calls /search/?tags=python&tags=fastapi&tags=api?
from fastapi import FastAPI, Query from typing import List app = FastAPI() @app.get("/search/") async def search(tags: List[str] = Query([])): return {"tags": tags}
Think about how FastAPI handles multiple query parameters with the same name when using a List type.
FastAPI collects all query parameters with the same name into a list when the type is List[str]. So the response contains all three tags as a list.
Examine this FastAPI endpoint:
from fastapi import FastAPI, Query
app = FastAPI()
@app.get("/users/")
async def get_user(age: int = Query(..., ge=0, le=120)):
return {"age": age}What is the reason for a 422 validation error when the client calls /users/?age=twenty?
from fastapi import FastAPI, Query app = FastAPI() @app.get("/users/") async def get_user(age: int = Query(..., ge=0, le=120)): return {"age": age}
Consider the data type expected and the actual input value.
The query parameter expects an integer. The string 'twenty' cannot be converted to int, so FastAPI returns a 422 validation error before checking range constraints.
FastAPI uses Pydantic models and field constraints to validate query parameters. Which statement correctly explains this process?
Think about when and how FastAPI applies validation rules declared with Query and Pydantic.
FastAPI uses Pydantic to parse and validate query parameters before calling the endpoint. It converts types and applies constraints like min_length, ge, le, etc. If validation fails, it returns a 422 error automatically.