401 Unauthorized vs 403 Forbidden in Rest API - Performance Comparison
When working with REST APIs, understanding how server responses behave helps us write better code. Here, we look at how the server decides to respond with 401 or 403 status codes.
We want to know how the server's decision process grows as it checks user credentials and permissions.
Analyze the time complexity of this simplified REST API authorization check.
function checkAccess(request) {
if (!request.hasValidToken()) {
return 401; // Unauthorized
}
if (!request.userHasPermission()) {
return 403; // Forbidden
}
return 200; // OK
}
This code checks if a request has a valid token, then checks if the user has permission to access the resource.
Look for repeated checks or loops in the code.
- Primary operation: Checking token validity and user permissions.
- How many times: Each check runs once per request; no loops or recursion.
The checks happen once per request regardless of input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 requests | 10 token checks + 10 permission checks |
| 100 requests | 100 token checks + 100 permission checks |
| 1000 requests | 1000 token checks + 1000 permission checks |
Pattern observation: The work grows linearly with the number of requests, but each request's checks stay constant.
Time Complexity: O(1)
This means each request is handled in constant time, no matter how complex the input data is.
[X] Wrong: "Checking permissions always takes longer as the number of users grows."
[OK] Correct: Permission checks here happen once per request and do not loop over all users, so time stays constant per request.
Understanding how authorization checks scale helps you design APIs that respond quickly and securely. This skill shows you can think about both correctness and performance.
"What if the permission check involved searching through a list of roles? How would the time complexity change?"
Practice
401 Unauthorized indicate in a REST API?Solution
Step 1: Understand the meaning of 401 Unauthorized
The 401 status code means the client must authenticate itself to get the requested response. It usually means no or bad login info.Step 2: Differentiate from 403 Forbidden
403 means the client is authenticated but lacks permission. 401 means no valid authentication.Final Answer:
The client is not authenticated or provided invalid credentials. -> Option AQuick Check:
401 Unauthorized = No or bad login info [OK]
- Confusing 401 with 403
- Thinking 401 means resource missing
- Assuming 401 means server error
Solution
Step 1: Identify the meaning of 403 Forbidden
403 means the user is authenticated but does not have permission to access the resource.Step 2: Confirm 401 is for unauthenticated users
401 is for users who are not logged in or have invalid credentials, so it does not apply here.Final Answer:
403 Forbidden -> Option CQuick Check:
403 Forbidden = Logged in but no permission [OK]
- Using 401 when user is logged in
- Confusing 403 with 404 Not Found
- Thinking 403 means server error
{"status":401,"message":"Invalid token"}What does this response tell the client?
Solution
Step 1: Analyze the status code 401
401 means unauthorized, so the client is not properly authenticated.Step 2: Interpret the message "Invalid token"
This indicates the authentication token is missing or invalid, so the client must provide valid credentials.Final Answer:
The client provided an invalid or missing authentication token. -> Option AQuick Check:
401 + "Invalid token" = Bad login info [OK]
- Thinking 401 means permission denied
- Assuming resource is missing
- Confusing 401 with server error
if (!user.isLoggedIn()) {
return 403;
} else if (!user.hasAccess()) {
return 401;
}What is wrong with this code?
Solution
Step 1: Check the meaning of 401 and 403
401 means unauthorized (not logged in), 403 means forbidden (no permission).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.Final Answer:
It returns 403 when user is not logged in, should be 401 instead. -> Option DQuick Check:
Not logged in = 401, no permission = 403 [OK]
- Swapping 401 and 403 codes
- Using 404 for unauthorized
- Assuming code is correct without checking status meanings
GET /files/123
The user is logged in but does not own the file. Which status code should your API return and why?
Solution
Step 1: Determine user's authentication and ownership
The user is logged in (authenticated) but does not own the file, so lacks permission.Step 2: Choose correct status code for permission denial
403 Forbidden is the correct code when the user is authenticated but not allowed to access the resource.Final Answer:
403 Forbidden, because the user is authenticated but not allowed to access this file. -> Option BQuick Check:
Logged in + no permission = 403 Forbidden [OK]
- Using 401 when user is logged in
- Using 404 to hide resource instead of 403
- Confusing 500 error with permission issues
Ready to prove it?
Pass the test and this pattern moves unknown
