Bird
0
0

What will be the response if the client sends an incorrect API key with the above code snippet?

medium📝 component behavior Q5 of 15
FastAPI - Authentication and Security
What will be the response if the client sends an incorrect API key with the above code snippet?
A200 OK with secure data
B403 Forbidden with detail 'Invalid API Key'
C401 Unauthorized error
D500 Internal Server Error
Step-by-Step Solution
Solution:
  1. Step 1: Review code error handling

    If the API key does not match 'secret123', the code raises HTTPException with status 403.
  2. Step 2: Confirm response status and message

    The exception detail is 'Invalid API Key', so client gets 403 Forbidden with this message.
  3. Final Answer:

    403 Forbidden with detail 'Invalid API Key' -> Option B
  4. Quick Check:

    Wrong API key = 403 Forbidden [OK]
Quick Trick: Wrong API key triggers 403 Forbidden error [OK]
Common Mistakes:
MISTAKES
  • Confusing 403 with 401 Unauthorized
  • Expecting 200 OK despite wrong key
  • Thinking server error occurs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes