401 vs 403: The Difference Nobody Bothers to Get Right

Ask five engineers what the difference between 401 Unauthorized and 403 Forbidden is, and you'll often get five slightly different answers, followed by a codebase where both are used interchangeably depending on who wrote the endpoint. The distinction isn't pedantic trivia — it changes what a client is supposed to do next, and getting it backwards teaches your frontend the wrong lesson.
What the spec actually says
The HTTP spec's own naming is arguably the reason this is confusing: 401 Unauthorized is really about authentication, not authorization. It means "I don't know who you are, or the credentials you gave me aren't valid — log in (or log in again) and try again." A 401 response is required to include a WWW-Authenticate header describing how to authenticate, which is a strong hint about its actual purpose: this is the "please authenticate" status, not the "you're not allowed" status.
403 Forbidden means the opposite kind of no: "I know exactly who you are, your credentials are fine, and the answer is still no." Logging in again won't help, because the problem isn't your identity — it's your permissions. Trying to view another user's private data while correctly logged in as yourself is a textbook 403, not a 401.
Why mixing them up actually breaks things
This isn't just a matter of correctness for its own sake. Client code frequently branches on this exact distinction:
- A
401commonly triggers "redirect to login" or "try refreshing the access token and retry the request" logic. - A
403should not trigger either of those — retrying with a fresh token accomplishes nothing if the real problem is a missing permission, and redirecting an already-logged-in user back to a login screen because they tried to open something they don't have access to is a confusing, wrong experience.
A backend that returns 401 for "you don't have permission" will send perfectly authenticated users back through a login flow that can't fix their actual problem. A backend that returns 403 for "your session expired" will show a permission-denied message to a user whose actual issue is that they just need to log back in — which reads as far more alarming and confusing than "please sign in again."
The other common confusion: 404 as a security choice
A related, deliberate pattern worth knowing: some APIs return 404 Not Found instead of 403 for a resource that exists but that the caller isn't allowed to see — specifically to avoid confirming the resource's existence to someone who shouldn't know about it. Whether a resource id exists at all can itself be sensitive information (a team id, a private document id), and 404 doesn't leak that the way 403 implicitly does ("it exists, you just can't have it"). This is a legitimate, common choice — it's just a different decision than "distinguish 401 from 403 correctly," and worth being deliberate about rather than an accident of whichever check happened to run first.
Testing that your frontend actually reacts differently
The only way to know your frontend's 401 and 403 handling actually diverge the way they're supposed to is to trigger both, independently, and watch what happens — which is exactly the kind of thing that's tedious to set up against a real backend (you'd need a real expired-but-valid-looking session for one case, and a real logged-in user without permission for the other) and trivial against a mock. Configuring one route to return 401 and a near-identical one to return 403 — or the same route to return either depending on a header or query param via conditional matching — lets you confirm your app's actual behavior in each case in seconds, instead of requiring two separately staged real-world account states.
Getting this right is a small thing, but it's the kind of small thing that shows up in support tickets as "your app logged me out for no reason" when the real story is a permissions check that returned the wrong status code.
Xerat02
Building MockBase.
More from the blog

Mock vs Stub vs Spy vs Fake: The Test Double Vocabulary Nobody Agrees On
Developers use "mock" as a catch-all for anything fake in a test, which is exactly backwards from how the term was originally meant — and the actual distinctions aren't pedantry, they're about what a test can and can't tell you when it passes.

301 vs 302: The Redirect Code That Decides Who Gets Your SEO
Both codes get a browser to the same place in the same tenth of a second — the difference only shows up in what a search engine does with the old URL, which is exactly the part manual testing never checks.