Consider this FastAPI code snippet:
from fastapi import FastAPI, Depends
app = FastAPI()
def common_parameters(q: str | None = None):
return {"q": q}
@app.get("/items/")
async def read_items(commons: dict = Depends(common_parameters)):
return commonsWhat will be the JSON response when a client requests /items/?q=hello?
from fastapi import FastAPI, Depends app = FastAPI() def common_parameters(q: str | None = None): return {"q": q} @app.get("/items/") async def read_items(commons: dict = Depends(common_parameters)): return commons
Think about how query parameters are passed and how the dependency function returns them.
The dependency common_parameters receives the query parameter q with value "hello" and returns it in a dictionary. The path operation returns this dictionary as JSON.
Given this FastAPI code:
from fastapi import FastAPI, Depends
app = FastAPI()
def get_user(token: str):
if token == "secret":
return "admin"
return "guest"
@app.get("/profile")
async def profile(user: str = Depends(get_user)):
return {"user": user}What will be the response when a client calls /profile?token=secret?
from fastapi import FastAPI, Depends app = FastAPI() def get_user(token: str): if token == "secret": return "admin" return "guest" @app.get("/profile") async def profile(user: str = Depends(get_user)): return {"user": user}
Check how the token query parameter is passed to the dependency.
The dependency get_user receives the token "secret" and returns "admin". The path operation returns this as the user.
Which of the following FastAPI dependency declarations will cause a syntax error?
Check how Depends is used as a callable.
Depends must be called as Depends() to create a dependency instance. Using Depends without parentheses is a syntax error in this context.
Given this FastAPI code:
from fastapi import FastAPI, Depends
app = FastAPI()
call_count = 0
def dep():
global call_count
call_count += 1
return call_count
@app.get("/count")
async def count_calls(a: int = Depends(dep), b: int = Depends(dep)):
return {"a": a, "b": b}How many times is dep called when a client requests /count?
from fastapi import FastAPI, Depends app = FastAPI() call_count = 0 def dep(): global call_count call_count += 1 return call_count @app.get("/count") async def count_calls(a: int = Depends(dep), b: int = Depends(dep)): return {"a": a, "b": b}
Each Depends call creates a separate dependency instance.
Each parameter uses Depends(dep) separately, so the function dep is called twice per request.
Examine this FastAPI code snippet:
from fastapi import FastAPI, Depends
app = FastAPI()
def get_token(token: str):
return token
@app.get("/secure")
async def secure_route(token: str = Depends(get_token)):
return {"token": token}When calling /secure without query parameters, what error occurs and why?
from fastapi import FastAPI, Depends app = FastAPI() def get_token(token: str): return token @app.get("/secure") async def secure_route(token: str = Depends(get_token)): return {"token": token}
Consider how FastAPI handles required query parameters in dependencies.
The dependency get_token requires a token query parameter. If missing, FastAPI returns HTTP 422 error indicating a validation failure.