# HTTP Status Codes

> The 3-digit integer codes (1xx to 5xx) returned by a server to indicate the outcome of an HTTP request.

Subject: Internet | Lesson 11 of 35 | 3 min read
Interactive lesson: https://codewithanimation.in/internet/http-status-codes
Previous: [HTTP Methods](https://codewithanimation.in/internet/http-methods.md) | Next: [HTTP Headers](https://codewithanimation.in/internet/http-headers.md)

## What it is

**HTTP Status Codes** are 3-digit integers returned by the server in the response Start Line. They are grouped into 5 classes: **1xx** (Informational), **2xx** (Success), **3xx** (Redirection), **4xx** (Client Error), and **5xx** (Server Error).

## Why it matters

They tell you exactly how to handle a response in the UI (e.g., showing a green checkmark for 200, redirecting the user on 301, showing a validation error on 400, or a 'try again later' message on 500). They also affect SEO (301 permanent vs 302 temporary redirects).

## How it works

The first digit of the status code defines its class of response. The browser's network stack parses this number immediately upon receiving the start line and reacts automatically to certain codes (like following a `Location` header on a 301).

## Common mistake: Common mistake

Treating all errors the same in the UI. Beginners often show a generic 'Something went wrong' for a 400 (which usually means the user typed something invalid) and a 500 (which means the server crashed). Also, confusing 401 (Unauthorized / needs login) with 403 (Forbidden / logged in, but not allowed).

## Tip: Pro tip

Use the **'Who is to blame?'** mental model. **2xx**: Here you go. **3xx**: Go over there. **4xx**: *You* screwed up (bad input, no auth). **5xx**: *I* (the server) screwed up.

## Try it: Try it

Use the Status Code Simulator below. Trigger different codes and see how the 'Blame Meter' shifts depending on the error class.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/internet/http-status-codes

## Check yourself

### Question 1

Which class of HTTP status codes indicates that the client made a mistake, such as requesting a page that doesn't exist?

- A. 4xx (Client Error)
- B. 2xx (Success)
- C. 3xx (Redirection)
- D. 5xx (Server Error)

Correct answer: A

4xx codes mean 'You screwed up'-the client sent a bad request, lacks authentication, or requested a non-existent resource (like a 404).

- Why not B: 2xx codes indicate the request was received, understood, and accepted successfully.
- Why not C: 3xx codes indicate further action needs to be taken by the user agent in order to fulfill the request.
- Why not D: 5xx codes indicate the server failed to fulfill an apparently valid request.

### Question 2

What is the primary difference between a 401 Unauthorized and a 403 Forbidden?

- A. 401 means you need to log in; 403 means you are logged in but lack permission.
- B. 401 is a client error; 403 is a server error.
- C. 401 means the API key is invalid; 403 means the URL is misspelled.
- D. There is no difference; they are interchangeable aliases.

Correct answer: A

Semantically, 401 means 'unauthenticated' (you haven't proven who you are). 403 means 'unauthorized' (we know who you are, but you don't have the rights to access this).

- Why not B: Both 401 and 403 are 4xx Client Errors.
- Why not C: A misspelled URL typically results in a 404 Not Found.
- Why not D: They have distinct semantic meanings regarding authentication vs authorization.

### Question 3

If your backend server crashes due to an unhandled exception while processing a request, which status code should it return?

- A. 500 Internal Server Error
- B. 400 Bad Request
- C. 200 OK
- D. 404 Not Found

Correct answer: A

5xx errors mean 'I (the server) screwed up.' A 500 Internal Server Error is the generic catch-all for when the server encounters an unexpected condition.

- Why not B: 400 implies the client sent bad data. If the server crashes on good data, it's a 500.
- Why not C: A crash is definitely not 'OK'.
- Why not D: 404 means the endpoint doesn't exist, not that it crashed during execution.

## Remember this

- 2xx = Success (e.g., 200 OK, 201 Created).
- 3xx = Redirection (e.g., 301 Moved Permanently, 302 Found).
- 4xx = Client Error (e.g., 400 Bad Request, 401 Unauthorized, 404 Not Found).
- 5xx = Server Error (e.g., 500 Internal Server Error, 502 Bad Gateway).
