The difference between HTTP status codes 401 and 403: Unauthorized and Forbidden
Tags: webdev, quicktip
Published: 2021-11-17 16:09:47

---
title: The difference between HTTP status codes 401 and 403: Unauthorized and Forbidden
published: true
date: 2021-11-17 09:00:00 UTC
tags: webdevelopment,quicktip
canonical_url: https://flaviabastos.ca/2021/11/17/the-difference-between-http-status-codes-401-and-403-unauthorized-and-forbidden/
---

You are working with an API and after sending a request you receive one of these two HTTP response codes: 401 or 403. What do HTTP status codes 401 and 403 mean?

## Official specification

If you search for the official specification, this is what you get:

### 401: Unauthorized

> The 401 (Unauthorized) status code indicates that the request has not been applied because it lacks valid authentication credentials for the target resource. 
> 
> <cite><a href="https://httpwg.org/specs/rfc7235.html#status.401">https://httpwg.org/specs/rfc7235.html#status.401</a></cite>

### 403: Forbidden

> The 403 (Forbidden) status code indicates that the server understood the request but refuses to authorize it. 
> 
> <cite><a href="https://httpwg.org/specs/rfc7231.html#status.403">https://httpwg.org/specs/rfc7231.html#status.403</a></cite>

Hmmm…. ![🤔](https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/72x72/1f914.png) These two sound pretty similar to me. And to make things even more confusing, 403 “Forbidden” says that the server refuses to “authorize” the request but it’s code 401 that is called “Unauthorized”. ![😵](https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/72x72/1f635.png)

<iframe src="https://giphy.com/embed/zrmTqopWm4W5cPg8Ah" title="https://giphy.com/gifs/theoffice-zrmTqopWm4W5cPg8Ah"></iframe>

## Let’s simplify: airport analogy

You need a valid flight ticket to board a plane and the flight ticket has some information: your name and the gate number, for example, along with the destination, flight time, etc.

To simplify this analogy, the ticket has two parts: your identification (a username and a password to access the API) and your gate number (the resource you want to access).

If the ticket is incorrect or damaged, you cannot even go through the airport security: when they check your ticket, it will be refused. You are **Forbidden** to enter the boarding area of the airport.

Next, let’s say that your ticket is correct (so you made through security just fine!) and the gate number in your ticket says “Gate 24” but you walk to Gate 27. The attendant **cannot authorize** you to go through that gate because it’s not the right gate for your ticket.

> In other words, an “incorrect ticket” is similar to messing up your credentials: wrong username and/or password and you receive back a 403 Forbidden. Using the correct credentials but trying to access a resource that is not allowed for those credentials returns you a 401 Unauthorized.

![](https://testfbdotblog.files.wordpress.com/2021/11/403-x-401.png?w=1024)

You can also think that 403 happens before 401, despite the natural number order: you will not receive a 401 until you resolve a 403.

## A real example

The screenshot below shows that I try to login to my headspace account with a incorrect password (a “damaged” ticket in our analogy) and the response code in the network tab of the developer tools shows the 403.

![screenshot of login page and developer tools network tab showing 403 status code](https://testfbdotblog.files.wordpress.com/2021/11/headspace.png?w=1024)
_403 HTTP status code when trying to login with incorrect username or password_

## Important! Don’t believe everything you read!

One important thing to note here: the official specification is **not enforced in any way** and like many other things in software development, you might find out that the response codes are not consistent with the specification. If you are using a web framework, this will be configured out of the box and the return codes will work as specified but developers can override the base implementation and assign whichever code they want as responses.

Of course this is a bad idea because it will lead to confusion and it’s prone to error.

It takes just one Google search to prove to you that people mix these 2 status codes up all.the.time and there’s a chance that the API you are using got things backwards!

Here’s a horror story for you: I once worked with an API that only used GET methods (for modifying resources as well, instead of POST!!) and all response codes, for anything, were 200. ![😱](https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/72x72/1f631.png) So! Watch out!

* * *

If you found this helpful, please share this article!

> HTTP 401 and 403 explained
> 
> [Tweet](http://twitter.com/share?&text=HTTP%20401%20and%20403%20explained&url=https://flaviabastos.ca/2021/11/17/the-difference-between-http-status-codes-401-and-403-unauthorized-and-forbidden/)

> _The post_ [The difference between HTTP status codes 401 and 403: Unauthorized and Forbidden](https://flaviabastos.ca/2021/11/15/the-difference-between-http-status-codes-401-and-403-unauthorized-and-forbidden/) _was originally published at _[flaviabastos.ca](https://flaviabastos.ca/)