HTTP Status Codes

Status Code Families

RangeMeaningCommon codes
1xxinformationalrarely seen in APIs
2xxsuccess200 OK, 201 Created, 204 No Content
3xxredirection301 Moved Permanently, 304 Not Modified
4xxclient error (the request is wrong)400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found, 405 Method Not Allowed, 409 Conflict
5xxserver error (your code or server failed)500 Internal Server Error, 502 Bad Gateway, 503 Service Unavailable

Which Code for Which Situation?

Example

A quick reference written as a Java switch expression, using ShopEase situations:

public class Main {
    static String describe(int code) {
        return switch (code / 100) {
            case 2 -> "success";
            case 3 -> "redirect";
            case 4 -> "client error";
            case 5 -> "server error";
            default -> "informational";
        };
    }

    public static void main(String[] args) {
        int[][] cases = { {200}, {201}, {204}, {400}, {401}, {403}, {404}, {409}, {500} };
        String[] situations = {
            "GET /api/products returned the list",
            "POST /api/orders created a new order",
            "DELETE /api/cart/7 removed the item",
            "price was negative in the request body",
            "no login token sent",
            "a CUSTOMER tried to delete a product",
            "product 999 does not exist",
            "email already registered",
            "database is down" };
        for (int i = 0; i < cases.length; i++) {
            int code = cases[i][0];
            System.out.printf("%d %-13s <- %s%n", code, describe(code), situations[i]);
        }
    }
}

Output

200 success       <- GET /api/products returned the list
201 success       <- POST /api/orders created a new order
204 success       <- DELETE /api/cart/7 removed the item
400 client error  <- price was negative in the request body
401 client error  <- no login token sent
403 client error  <- a CUSTOMER tried to delete a product
404 client error  <- product 999 does not exist
409 client error  <- email already registered
500 server error  <- database is down

Note: 401 means "who are you?" (not logged in). 403 means "I know who you are, but you are not allowed". This difference is a favourite interview question.