Bird
0
0

Given this FastAPI endpoint with role check dependency:

medium📝 component behavior Q13 of 15
FastAPI - Authentication and Security
Given this FastAPI endpoint with role check dependency:
async def get_admin_data(admin: None = Depends(admin_required)):
    return {"data": "secret"}
What happens if a user with role 'user' calls this endpoint?
AThe endpoint raises HTTP 403 Forbidden error
BThe endpoint returns {"data": "secret"}
CThe endpoint raises HTTP 401 Unauthorized error
DThe endpoint returns an empty response
Step-by-Step Solution
Solution:
  1. Step 1: Understand admin_required behavior

    admin_required raises HTTP 403 if user role is not 'admin'.
  2. Step 2: Apply to user role 'user'

    User role 'user' is not 'admin', so HTTP 403 is raised before endpoint runs.
  3. Final Answer:

    The endpoint raises HTTP 403 Forbidden error -> Option A
  4. Quick Check:

    Non-admin user triggers 403 error [OK]
Quick Trick: Non-admin roles cause 403 error before endpoint runs [OK]
Common Mistakes:
MISTAKES
  • Confusing 401 Unauthorized with 403 Forbidden
  • Expecting endpoint to return data for non-admin
  • Thinking empty response is returned

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes