Bird
0
0

Given this FastAPI code snippet, what will be the output if the JWT token is expired?

medium📝 component behavior Q4 of 15
FastAPI - Authentication and Security
Given this FastAPI code snippet, what will be the output if the JWT token is expired?
from fastapi import Depends, HTTPException
from fastapi.security import OAuth2PasswordBearer
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
async def verify_token(token: str = Depends(oauth2_scheme)):
if token == "expired":
raise HTTPException(status_code=401, detail="Token expired")
return "Valid token"
result = await verify_token("expired")
ARaises HTTPException with status 401 and detail 'Token expired'
BReturns string 'Valid token'
CRaises HTTPException with status 403
DReturns None
Step-by-Step Solution
Solution:
  1. Step 1: Check token value in function

    If token equals "expired", function raises HTTPException with 401 status.
  2. Step 2: Analyze call with token "expired"

    Calling with "expired" triggers the exception, no return string.
  3. Final Answer:

    Raises HTTPException with status 401 and detail 'Token expired' -> Option A
  4. Quick Check:

    Expired token triggers 401 HTTPException [OK]
Quick Trick: Expired JWT tokens cause 401 Unauthorized error [OK]
Common Mistakes:
MISTAKES
  • Expecting 'Valid token' return on expired token
  • Confusing 401 with 403 status code
  • Assuming function returns None on error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes