Bird
0
0

You have this REST API code snippet:

medium📝 Debug Q14 of 15
Rest API - HTTP Status Codes
You have this REST API code snippet:
if (!user.isLoggedIn()) {
  return 403;
} else if (!user.hasAccess()) {
  return 401;
}

What is wrong with this code?
AIt returns 401 when user lacks access, should be 403 instead.
BThe code is correct as is.
CIt should return 404 for unauthorized access.
DIt returns 403 when user is not logged in, should be 401 instead.
Step-by-Step Solution
Solution:
  1. Step 1: Check the meaning of 401 and 403

    401 means unauthorized (not logged in), 403 means forbidden (no permission).
  2. Step 2: Analyze the code logic

    The code returns 403 when user is not logged in, which is incorrect. It should return 401 here. It returns 401 when user lacks access, which should be 403.
  3. Final Answer:

    It returns 403 when user is not logged in, should be 401 instead. -> Option D
  4. Quick Check:

    Not logged in = 401, no permission = 403 [OK]
Quick Trick: 401 for no login, 403 for no permission [OK]
Common Mistakes:
  • Swapping 401 and 403 codes
  • Using 404 for unauthorized
  • Assuming code is correct without checking status meanings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes