HTTP Status Code Classification and Common Status Code Analysis

HTTP Status Code Classification and Common Status Code Analysis

Topic Description
HTTP status codes are identifiers of a server's response to a client's request, consisting of a three-digit number and a descriptive text. They are divided into five major categories, each representing a different type of response status. Understanding the meaning of status codes helps in quickly identifying the result of a request (such as success, failure, redirection, etc.) and efficiently solving problems during development or debugging.


1. The Five Major Classifications of HTTP Status Codes
The first digit of the status code determines the category, while the last two digits specify the particular status:

  • 1xx (Informational): The request has been received and requires continued processing (e.g., 100 Continue).
  • 2xx (Success): The request has been successfully processed (e.g., 200 OK).
  • 3xx (Redirection): Further action is required to complete the request (e.g., 301 Moved Permanently).
  • 4xx (Client Error): The request contains a syntax error or cannot be completed (e.g., 404 Not Found).
  • 5xx (Server Error): The server failed to process the request (e.g., 500 Internal Server Error).

Key Point: The first digit directly corresponds to the category, allowing for quick problem orientation by remembering the classification pattern.


2. Detailed Explanation of Common Status Codes
(1)2xx Success Class Examples

  • 200 OK: The request succeeded, and the response body contains the specific result (e.g., webpage HTML or API data).
  • 201 Created: The resource was successfully created (common after a POST request, such as creating a new user).
  • 204 No Content: The request succeeded, but there is no content in the response body (e.g., after a successful delete operation).

(2)3xx Redirection Class Examples

  • 301 Moved Permanently: The resource has permanently moved to a new URL, and the client should update bookmarks.
  • 302 Found: The resource is temporarily redirected; the original URL may still be used for future requests.
  • 304 Not Modified: The resource has not been modified, and the client can use the cached version (requires headers like If-Modified-Since).

(3)4xx Client Error Examples

  • 400 Bad Request: The request contains a syntax error (e.g., malformed JSON).
  • 401 Unauthorized: Authentication is required (e.g., accessing a restricted resource without logging in).
  • 403 Forbidden: The server understood the request but refuses to execute it (e.g., insufficient permissions).
  • 404 Not Found: The resource does not exist; check if the URL is correct.

(4)5xx Server Error Examples

  • 500 Internal Server Error: An internal server error occurred (e.g., code exception).
  • 502 Bad Gateway: The gateway or proxy server received an invalid response from an upstream server.
  • 503 Service Unavailable: The service is temporarily unavailable (e.g., server overload).

3. Troubleshooting Logic in Practical Scenarios
Step 1: Locate the Responsible Party Based on the First Digit

  • Receiving a 4xx: Prioritize checking the client request (e.g., parameters, URL, permissions).
  • Receiving a 5xx: Contact the server side for investigation (e.g., database connection, code logic).

Step 2: Refine Analysis with Specific Status Codes

  • If 401 is returned: Check if the request headers carry a valid Token.
  • If 500 is returned: Examine the exception stack trace in the server logs.

Step 3: Handling Special Status Codes

  • 301/302: Check the Location field in the response headers to obtain the new URL.
  • 304: Optimize caching strategies to reduce redundant transmissions.

4. High-Frequency Interview Extension Questions

  • Difference Between 301 and 302: 301 is used for permanent moves (SEO weight transfer), while 302 is for temporary redirects (e.g., campaign pages).
  • How to Debug 500 Errors: Check server logs, status of dependent services, and code exception handling mechanisms.
  • Difference Between 403 and 401: 401 indicates unauthenticated (requires login), while 403 indicates authenticated but insufficient permissions.

By memorizing classifications and associating them with scenarios, one can systematically master the application and troubleshooting techniques of status codes.