Bird
0
0

Given this code snippet, what is the value of 'token' inside the endpoint if the Authorization header is 'Bearer abc123'?

medium📝 component behavior Q5 of 15
FastAPI - Authentication and Security
Given this code snippet, what is the value of 'token' inside the endpoint if the Authorization header is 'Bearer abc123'?
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")

@app.get("/items/")
async def read_items(token: str = Depends(oauth2_scheme)):
    return {"token": token}
ANone
B"Bearer abc123"
C"abc123"
D"token"
Step-by-Step Solution
Solution:
  1. Step 1: Understand OAuth2PasswordBearer token extraction

    It extracts only the token string after 'Bearer ' prefix from Authorization header.
  2. Step 2: Analyze given header

    Header is 'Bearer abc123', so token is 'abc123'.
  3. Final Answer:

    "abc123" -> Option C
  4. Quick Check:

    Extracted token = "abc123" [OK]
Quick Trick: OAuth2PasswordBearer extracts token after 'Bearer ' prefix [OK]
Common Mistakes:
MISTAKES
  • Including 'Bearer ' in token value
  • Assuming token is None if present
  • Returning literal string 'token'

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes