Bird
0
0

Given this FastAPI dependency:

medium📝 component behavior Q13 of 15
FastAPI - Authentication and Security
Given this FastAPI dependency:
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")

@app.get("/users/me")
async def read_users_me(token: str = Depends(oauth2_scheme)):
    return {"token": token}
What will be the output if the client sends a request with header Authorization: Bearer abc123?
A{"token": "Bearer abc123"}
BHTTP 401 Unauthorized error
C{"token": "abc123"}
D{"token": null}
Step-by-Step Solution
Solution:
  1. Step 1: Understand OAuth2PasswordBearer behavior

    This class extracts only the token string after 'Bearer ' from the Authorization header.
  2. Step 2: Analyze the returned value

    The function returns a JSON with the token string, so it will return {"token": "abc123"}.
  3. Final Answer:

    {"token": "abc123"} -> Option C
  4. Quick Check:

    Bearer token string extracted = "abc123" [OK]
Quick Trick: OAuth2PasswordBearer strips 'Bearer ' prefix automatically [OK]
Common Mistakes:
MISTAKES
  • Expecting full 'Bearer abc123' string returned
  • Assuming 401 error without token validation
  • Thinking token is null if present

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes