Status Code Ranges 2xx 3xx 4xx 5xx

Learn what HTTP status code ranges mean, the most common codes in each range, and how to use them correctly in web applications.

When a browser or any client sends an HTTP request to a server, the server must send back a response. That response always starts with a three‑digit number called a status code. This number tells the client, in a very compact way, whether the request succeeded, failed, or needs something else to happen. For example, if you visit a webpage, your browser sends a GET request. If the page exists, the server replies with 200 OK. If the page doesn't exist, the server replies with 404 Not Found. The status code is the single most important piece of information in the response because it tells the client what to do next.

What a Status Code Looks Like

Every HTTP response begins with a status line that looks like this:

HTTP/1.1 200 OK

The first part (HTTP/1.1) is the protocol version. The second part (200) is the numeric status code. The third part (OK) is a short human‑readable reason phrase that describes the code. The reason phrase is optional in some newer versions of HTTP, but the numeric code is always required.

The client (a browser, a mobile app, another server) reads this number and decides how to react. A 200 tells the client "everything went well, you can use the body of this response." A 404 tells the client "the thing you asked for is not here; maybe show a not‑found page." A 301 tells the client "this resource has moved permanently; please go to this other URL instead."

The First Digit Defines the Category

All status codes are exactly three digits. The first digit tells you which of five broad categories the code belongs to:

  • 1xx - Informational: The request was received and the server is continuing to process it. These are rarely seen in normal web traffic.
  • 2xx - Success: The request was received, understood, and accepted.
  • 3xx - Redirection: The client must take additional action to complete the request, usually by following a new URL.
  • 4xx - Client Error: The request contains bad syntax or cannot be fulfilled because of something on the client's side.
  • 5xx - Server Error: The server failed to fulfill a valid request.

This document focuses on the four ranges you will use constantly: 2xx, 3xx, 4xx, and 5xx. You can safely ignore the 1xx range for now; it is used internally for things like protocol switching and rarely appears in application code.

The 2xx Range - Success

A 2xx code means the request was processed successfully. The server is happy, and the client can proceed. But not all successes are the same. Different codes tell the client what kind of success occurred, and sometimes whether there is a response body to read.

200 OK - The Most Common Success

200 OK is the generic success code. It means "the request succeeded, and here is the result you asked for." It is the default response for most GET requests, many POST requests, and sometimes other methods when the server wants to return a body.

Here is a raw HTTP response with 200 OK:

HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 13
Hello, World!

In this example, a client asked for some resource, and the server returned 200 with a plain text body. The client knows the request worked and can display the body.

201 Created - Something New Was Made

201 Created is used when the request resulted in a new resource being created. This is common in APIs after a successful POST request that adds a new record to a database. The response often includes a Location header that points to the newly created resource.

Example response after creating a new user with POST /users:

HTTP/1.1 201 Created
Location: /users/123
Content-Type: application/json
{
  "id": 123,
  "name": "Alice"
}

The client sees 201 and knows "I created something, and this is where it lives now." A 200 would also mean success, but 201 gives the client extra information: the resource was created, and here is its URL.

202 Accepted - Work Is in Progress

202 Accepted means the server has received the request and will process it later, but it is not finished yet. This is useful for long‑running operations, such as sending an email, generating a report, or processing a video upload. The server acknowledges the request but does not yet have a final result.

Example:

HTTP/1.1 202 Accepted
Content-Type: application/json
{
  "status": "processing",
  "jobId": "abc-123"
}

The client should not expect a final result immediately. It might poll a different endpoint later to check on the job's status.

204 No Content - Success Without a Body

204 No Content means the request succeeded, but the server has nothing to send back. There is no response body at all. This is often used for DELETE requests when a resource is removed, or for PUT/PATCH updates that don't need to return data.

Example after DELETE /users/123:

HTTP/1.1 204 No Content

Notice there is no Content-Type or body. The client knows the deletion worked because of the 204, and there is nothing else to read.

Correct Use of 204:

If your handler successfully deletes a resource and has nothing meaningful to return, a 204 is cleaner than a 200 with an empty body. It tells the client "done, nothing to see here" without wasting bandwidth.

The 3xx Range - Redirection

A 3xx code tells the client that the resource it asked for is somewhere else, or that the client should use a cached version instead of making a new request. These codes usually come with a Location header that gives the new URL.

301 Moved Permanently - The URL Changed Forever

301 Moved Permanently means the resource has moved to a new URL, and that new URL is permanent. Browsers and search engines remember this redirect for a long time. If a user bookmarks the old URL, future visits will automatically go to the new one without asking the server again.

Example response when a page moved from /old-page to /new-page:

HTTP/1.1 301 Moved Permanently
Location: /new-page

The client should update any stored references to this resource.

302 Found - Temporary Redirect

302 Found is a temporary redirect. The resource is currently available at a different URL, but that may change later. The client should still use the original URL for future requests.

Example response:

HTTP/1.1 302 Found
Location: /temporary-location

A common use case is redirecting a user to a login page if they try to access a protected area, then returning them to the original page after login. The redirect is temporary because after login, the original URL works again.

303 See Other - the Redirect That Always Switches to GET

303 See Other is the redirect that intentionally changes the request method to GET. It is the code behind the POST/Redirect/GET pattern: after a form is submitted with POST, the server answers 303 with a Location header, and the browser fetches the results page with a GET, so refreshing the page does not resubmit the form. Unlike 301 and 302, where the method change is a legacy accident, with 303 the change to GET is the whole point. In Axum you return it with Redirect::to(url), which produces a 303 See Other response.

304 Not Modified - Use Your Cached Copy

304 Not Modified is special. It does not redirect anywhere. Instead, it tells the client "the resource you have cached is still valid; use it." This happens when the client sends a conditional request, usually with an If-None-Match or If-Modified-Since header. If the resource hasn't changed, the server responds with 304 and no body, saving bandwidth.

Example interaction:

  1. Client requests /logo.png and receives 200 OK with the image and an ETag header.
  2. Later, client requests /logo.png again, sending If-None-Match: "abc123".
  3. If the image hasn't changed, server responds:
HTTP/1.1 304 Not Modified

The client uses its cached copy instead of downloading the image again.

307 Temporary Redirect and 308 Permanent Redirect

307 and 308 are newer versions of 302 and 301. The key difference is that 307 and 308 preserve the HTTP method of the original request: the client must repeat the request with the same method and the same body. If a client sends a POST to /submit and the server responds with 307, the follow-up request is still a POST.

301 and 302 give no such guarantee. The specification allows the client to change the method, and in practice user agents do change a POST to a GET (and drop the body) when following a 301 or 302 - that is how browsers have behaved for decades. So "modern browsers handle 301 and 302 correctly" is the wrong way to think about it: for a plain GET the difference is invisible, but for a POST, PUT, or DELETE you cannot rely on the method surviving a 301 or 302. If the method must survive the redirect, use 307 (temporary) or 308 (permanent) instead. If you want the method to become GET, say so explicitly with 303.

301 vs 302 - A Common Mistake:

Using 301 for a temporary redirect can cause real problems. Browsers cache 301 responses aggressively. If you temporarily move a page but the user's browser remembers the 301, they will never ask the original URL again, even after you move the page back. Use 302 (or 307) for temporary moves, and reserve 301 (or 308) for permanent changes.

The 4xx Range - Client Error

A 4xx code means the client made a mistake. The server understood the request but refuses to process it because something is wrong with what the client sent. These are your tools for telling the user "you did something wrong, fix it and try again."

400 Bad Request - The Request Is Malformed

400 Bad Request means the server cannot understand the request because its syntax is invalid. This could be a malformed JSON body, a missing required header, or an invalid query parameter.

Example: A client sends a POST with invalid JSON:

POST /users HTTP/1.1
Content-Type: application/json
{ "name": "Alice", }   <-- trailing comma makes it invalid JSON

Server responds:

HTTP/1.1 400 Bad Request
Content-Type: text/plain
Invalid JSON: trailing comma at line 1

The client should fix the JSON and resend.

401 Unauthorized - Authentication Required

401 Unauthorized means the client must authenticate itself before the request can be processed. This often means "you need to log in" or "you need to provide a valid API token." The response should include a WWW-Authenticate header that tells the client how to authenticate.

Example:

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer

The client should obtain credentials and retry with an Authorization header.

403 Forbidden - You Are Authenticated but Not Allowed

403 Forbidden is often confused with 401, but they mean different things. 401 says "I don't know who you are, please identify yourself." 403 says "I know who you are, and you are not allowed to do this." Even if you are logged in, you might not have permission to access a certain resource.

Example: A regular user tries to access an admin panel.

HTTP/1.1 403 Forbidden
Content-Type: text/plain
You do not have permission to access this resource.

The client should not retry with different credentials; the current user simply lacks permission.

404 Not Found - The Resource Does Not Exist

404 Not Found means the requested URL does not map to any resource on the server. This is probably the most recognized status code outside of 200. It can also be used when the server wants to hide the existence of a resource (for privacy reasons), even if the resource technically exists but the user shouldn't know about it.

Example:

HTTP/1.1 404 Not Found
Content-Type: text/plain
The page you requested was not found.

405 Method Not Allowed - Wrong HTTP Method

405 Method Not Allowed means the URL exists, but the HTTP method used is not supported for that resource. For example, a resource might only support GET, but the client sends DELETE. The response should include an Allow header listing the methods that are allowed.

Example:

HTTP/1.1 405 Method Not Allowed
Allow: GET, HEAD

The client should change the method or use a different endpoint.

422 Unprocessable Entity - Semantically Invalid

422 Unprocessable Entity means the request was syntactically correct (unlike 400) but contains semantic errors. This is common in APIs that validate input data. For example, a user registration form might be well‑formed JSON, but the email address is already in use, or the password is too short.

Example:

HTTP/1.1 422 Unprocessable Entity
Content-Type: application/json
{
  "errors": {
    "email": "already taken",
    "password": "must be at least 8 characters"
  }
}

422 tells the client "your request is fine, but the data you sent doesn't make sense for this operation."

429 Too Many Requests - Slow Down and Retry Later

429 Too Many Requests means the client has sent more requests than the server is willing to handle in a given window. It is the standard response for rate limiting. Unlike most 4xx codes, a 429 is often temporary: the response usually carries a Retry-After header telling the client how many seconds to wait before trying again.

Example:

HTTP/1.1 429 Too Many Requests
Retry-After: 60
Content-Type: text/plain
You have sent too many requests. Please wait 60 seconds and try again.

A well-behaved client reads Retry-After and waits instead of hammering the server again immediately. Sending more requests right after a 429 only extends the wait.

Other 4xx codes worth recognising

A few more client-error codes appear often enough to know by sight. 408 Request Timeout means the client took too long to send its request. 409 Conflict means the request conflicts with the current state of the resource, for example trying to create a user with an email that already exists when the API treats that as a state conflict rather than a validation error. 413 Payload Too Large means the request body exceeded the server's size limit - Axum's default body limit is 2 MB, and a larger body is rejected before your handler runs. 451 Unavailable For Legal Reasons means the resource is withheld for legal reasons, such as a takedown request.

Never Return 200 with an Error Body:

Some developers mistakenly return 200 OK along with a JSON body like { "success": false, "error": "..." }. This is confusing and dangerous. HTTP clients, caches, and proxies rely on the status code to decide how to handle the response. A 200 with an error message may be cached, retried, or treated as a success by automated tools. Always use the correct 4xx or 5xx code to signal failure.

The 5xx Range - Server Error

A 5xx code means the server itself failed to fulfill a valid request. The client did nothing wrong; the problem is on the server's side. These errors are usually unexpected and should be logged and monitored.

500 Internal Server Error - Something Went Wrong, We Don't Know What

500 Internal Server Error is the generic server error. It means the server encountered an unexpected condition that prevented it from fulfilling the request. This could be a bug in the code, an unhandled exception, or a missing configuration. The server should not reveal details about the error in the response body; that information goes into the server's logs.

Example:

HTTP/1.1 500 Internal Server Error
Content-Type: text/plain
Something went wrong on our end. Please try again later.

502 Bad Gateway - Upstream Server Gave a Bad Response

502 Bad Gateway occurs when a server acting as a gateway or proxy receives an invalid response from an upstream server. This often happens in microservice architectures where one service calls another service that crashes or returns malformed data.

Example: A load balancer forwards a request to a backend service, but the backend returns a response the load balancer cannot parse.

HTTP/1.1 502 Bad Gateway

503 Service Unavailable - Temporarily Down

503 Service Unavailable means the server is temporarily unable to handle the request, usually because it is overloaded or under maintenance. Unlike 500, this implies a temporary condition that might resolve soon. The response may include a Retry-After header indicating when the client can try again.

Example:

HTTP/1.1 503 Service Unavailable
Retry-After: 60

504 Gateway Timeout - Upstream Server Did Not Respond

504 Gateway Timeout is similar to 502, but instead of a bad response, the upstream server did not respond at all within the allotted time. This can happen when a downstream service is extremely slow or hangs.

Example:

HTTP/1.1 504 Gateway Timeout

5xx Codes Are Not for the Client:

It's tempting to return a 500 when the client sends invalid data, but that is wrong. A 500 says "the server broke," not "the client sent bad data." If the problem is with the request, use a 4xx code. Reserve 5xx for genuine server failures.

How to Think About Status Codes

Think of status codes as a contract between the client and the server. The client sends a request, and the server must reply with a code that accurately describes the outcome. This contract lets clients automate decisions:

  • 2xx → "Success, proceed with the response body."
  • 3xx → "Go somewhere else, or use your cache."
  • 4xx → "You made a mistake, fix your request."
  • 5xx → "The server failed, try again later or report the problem."

Without accurate status codes, clients would have to guess what happened. A browser wouldn't know whether to show a page, redirect, or display an error. An API client wouldn't know whether to retry, cache, or show a validation message. Status codes make the web predictable and machine‑readable.

Returning Status Codes in Axum

So far this page has shown status codes as raw text on the wire. In Axum you never write that text yourself - you return a value and Axum builds the status line for you. The simplest way is to return a StatusCode from a handler, or a tuple of a status code and a body:

src/main.rs
use axum::{
    http::StatusCode,
    response::Redirect,
    routing::{get, post},
    Router,
};

async fn health() -> StatusCode {
    StatusCode::OK
}

async fn created() -> (StatusCode, &'static str) {
    (StatusCode::CREATED, "User created")
}

async fn missing() -> (StatusCode, &'static str) {
    (StatusCode::NOT_FOUND, "No user with that id")
}

async fn moved() -> Redirect {
    Redirect::to("/new-location")
}

#[tokio::main]
async fn main() {
    let app = Router::new()
        .route("/health", get(health))
        .route("/users", post(created))
        .route("/users/99", get(missing))
        .route("/old-page", get(moved));
    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000")
        .await
        .unwrap();
    axum::serve(listener, app).await.unwrap();
}

Each handler maps to one of the codes from this page. health answers 200 OK with no body, which is what a load balancer or monitoring tool checks. created answers 201 Created with a short message, the right response after a successful POST. missing answers 404 Not Found with an explanation. moved uses Redirect::to, which answers 303 See Other with a Location header - the browser then fetches /new-location with a GET. If you need a redirect that keeps the method, use Redirect::temporary for 307 or Redirect::permanent for 308 instead.

Pick the code before you write the body:

Decide the status code first, then write the body to match it. A handler that returns a 200 with an error message in the body breaks the contract this page describes, and no client can tell from the status line that anything went wrong. If the outcome is an error, return the 4xx or 5xx code that names it.

Common Misconceptions

Misconception 1: "200 Means Everything Is Fine"

Just because a request returned 200 does not mean the user got what they wanted. Some poorly built APIs return 200 OK with an error message in the body. This breaks the contract and forces clients to parse the body to detect errors. Always use the correct error code.

Misconception 2: "401 and 403 Are Interchangeable"

401 means "authentication missing or wrong." 403 means "authenticated but forbidden." For example, if you try to access an admin page while logged out, a 401 is appropriate. If you are logged in as a regular user, a 403 is appropriate. Mixing them up confuses clients and can cause security issues.

Misconception 3: "All Redirects Are Temporary"

301 is permanent, 302 is temporary. Using the wrong one can break bookmarks and search engine rankings. If a page moves permanently, use 301. If it moves temporarily, use 302. If you need to preserve the HTTP method, use 307 or 308.

Misconception 4: "404 Only Means Page Not Found"

404 can also be used to intentionally hide the existence of a resource. If a user requests /admin but you don't want to reveal whether that path exists, returning 404 instead of 403 is a privacy‑enhancing choice.

Real‑World Example: A Complete Request-Response Flow

Imagine a client tries to view a user profile at GET /users/123. Here is the sequence of possible responses:

  1. The user exists and the client has permission → 200 OK with the profile data.
  2. The user does not exist → 404 Not Found.
  3. The client is not logged in → 401 Unauthorized with a prompt to log in.
  4. The client is logged in but not allowed to view this profile → 403 Forbidden.
  5. The server's database is down → 500 Internal Server Error or 503 Service Unavailable.
  6. The profile has moved to a different URL → 301 Moved Permanently with a Location header.

Each status code gives the client a clear instruction on what to do next.

A Text Diagram of Status Code Decision Flow

Client sends request
        |
        v
Was the request understood?
   |-- No (bad syntax) --> 400 Bad Request
   |-- Yes
        |
        v
Does the resource exist?
   |-- No --> 404 Not Found
   |-- Yes
        |
        v
Does the client need to authenticate?
   |-- Yes, and not authenticated --> 401 Unauthorized
   |-- Yes, authenticated but forbidden --> 403 Forbidden
   |-- No or authenticated correctly
        |
        v
Did the server process it successfully?
   |-- Yes, with body --> 200 OK
   |-- Yes, created new resource --> 201 Created
   |-- Yes, no body to return --> 204 No Content
   |-- No, server error --> 5xx (500, 502, 503, 504)

This is a simplified mental model. Real applications have more nuanced decisions, but this covers the most common paths.

Summary

You now know that every HTTP response carries a three‑digit status code, and the first digit tells you the category: 2xx for success, 3xx for redirection, 4xx for client errors, and 5xx for server errors. Within each range, specific codes carry extra meaning - 201 means "created," 301 means "moved permanently," 401 means "not authenticated," and so on.

The single most important takeaway is this: the status code is the primary signal of what happened, not the response body. A well‑behaved server always returns the most accurate code possible. As you move forward in learning Axum, you will see how handlers set these codes, and how error handling uses 4xx and 5xx codes to tell clients what went wrong. Understanding these ranges now will make that next step much easier.