Bird
0
0

Given the code below, what will be the output when accessing the /items/ endpoint?

medium📝 component behavior Q13 of 15
FastAPI - Dependency Injection
Given the code below, what will be the output when accessing the /items/ endpoint?
from fastapi import FastAPI, Depends

app = FastAPI()

def get_number():
    return 42

@app.get('/items/')
def read_items(number: int = Depends(get_number)):
    return {"number": number}
A{"number": "get_number"}
B{"number": 42}
CError: missing required parameter
D{"number": null}
Step-by-Step Solution
Solution:
  1. Step 1: Understand dependency injection

    The get_number function returns 42, and FastAPI injects this value into the number parameter.
  2. Step 2: Check the returned response

    The route returns a dictionary with key "number" and value 42, so the output is {"number": 42}.
  3. Final Answer:

    {"number": 42} -> Option B
  4. Quick Check:

    Depends injects 42 = {"number": 42} [OK]
Quick Trick: Depends calls function and injects return value [OK]
Common Mistakes:
MISTAKES
  • Expecting the function name instead of its return value
  • Thinking parameter is missing if not passed explicitly
  • Assuming null is returned if no argument given

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes