0
0
FastAPIframework~20 mins

Global dependencies in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI Global Dependency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this FastAPI app with global dependencies?

Consider this FastAPI app where a global dependency adds a header. What will the response headers contain when you call /items/42?

FastAPI
from fastapi import FastAPI, Depends, Header

app = FastAPI()

def common_header(x_token: str = Header(...)):
    return x_token

@app.get("/items/{item_id}", dependencies=[Depends(common_header)])
async def read_item(item_id: int):
    return {"item_id": item_id}
AThe response includes header 'x-token' with the value sent by the client.
BThe response includes header 'x-token' with value 'None'.
CThe response is 422 Unprocessable Entity because header 'x-token' is missing.
DThe response body includes the token value under key 'x_token'.
Attempts:
2 left
💡 Hint

Think about what happens if the required header is missing in the request.

state_output
intermediate
2:00remaining
What is the value of the dependency result in this global dependency example?

Given this FastAPI app, what will be the value of token_value inside the endpoint?

FastAPI
from fastapi import FastAPI, Depends

app = FastAPI()

def get_token():
    return "secret-token"

@app.get("/users/me", dependencies=[Depends(get_token)])
async def read_user(token_value: str = Depends(get_token)):
    return {"token": token_value}
A"secret-token"
BNone
CRaises a runtime error due to duplicate dependency
DEmpty string
Attempts:
2 left
💡 Hint

Consider how FastAPI resolves dependencies when the same function is used globally and locally.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a global dependency in FastAPI?

Choose the correct way to add a global dependency that runs for every request.

Aapp = FastAPI(dependencies=[Depends(common_dep)])
B
app = FastAPI()
@app.global_dependency
async def common_dep(): pass
C
app = FastAPI()
app.add_dependency(common_dep)
D
app = FastAPI()
app.include_dependency(common_dep)
Attempts:
2 left
💡 Hint

Check FastAPI docs for how to add global dependencies at app creation.

🔧 Debug
advanced
2:00remaining
Why does this global dependency not run for the endpoint?

Given this code, why is the global dependency verify_token not executed when calling /open?

FastAPI
from fastapi import FastAPI, Depends

def verify_token():
    print("Token verified")

app = FastAPI(dependencies=[Depends(verify_token)])

@app.get("/open")
async def open_endpoint():
    return {"message": "Open to all"}
ABecause the endpoint does not have any path parameters.
BBecause the global dependency runs but print output is not visible in response.
CBecause the global dependency is only called if the endpoint has a matching parameter.
DBecause the global dependency is ignored for endpoints without explicit Depends in signature.
Attempts:
2 left
💡 Hint

Think about where print output goes in FastAPI apps.

🧠 Conceptual
expert
3:00remaining
How does FastAPI handle multiple global dependencies with overlapping parameters?

Suppose you add two global dependencies that both require a header x-token. How does FastAPI handle this situation?

AFastAPI calls only the first global dependency and skips the second.
BFastAPI requires the header twice and expects two different values.
CFastAPI raises a runtime error due to duplicate header dependencies.
DFastAPI calls both dependencies once each, sharing the same header value from the request.
Attempts:
2 left
💡 Hint

Consider how FastAPI resolves dependencies and parameters from requests.