There are many different types of HTTP status codes, but in practice you only need to master a few common ones, summarized below.

Category of the status code

HTTP status codes fall into the following five categories

Name category The reason the phrase
1XX Information Indicates the Information status code The accepted request is being processed
2XX Success Indicates the Success status code The request is successfully processed
3XX Redirection Redirection status code Additional action is required to complete the request
4XX CLient Error Indicates the CLient Error status The server cannot process the request (there is a problem with the client request
5XX Server Error Indicates the Server Error status code The server failed to process the request

14 commonly used status codes

2XX The request is successfully processed

200 OK

204 No Content

The request received by the server is successfully processed, but the response message returned does not contain the body part of the entity. Also, it is not allowed to return the body of any entity.

When 204 NoContent is returned after the request is processed from the browser, the page displayed by the browser is not updated

You should be prepared for Partial Content.

The client makes a scope request, and the server successfully executes that part of the GET request.

The response message contains the entity Content in the Range specified by content-Range.

3XX redirection (the browser needs to perform some special processing to properly process the request)

301 Moved Permanently Permanently

Permanent redirection, indicating that the requested resource has been assigned a new URI and should use the URI to which the resource now refers.

That is, if you have already bookmarked the URI corresponding to the resource, you should save it again as prompted by the Location header field. Bookmarks need to be updated.

302 Found (Temporary Redirection)

Temporary redirect, which indicates that the requested resource has been assigned a new URI and the user is expected to access it using the new URI.

Similar to a 301 permanent redirect, 302 represents a resource that is not permanently moved, but only temporarily. (It may continue to move and change urIs later.

303 See Other

The GET method should be used to GET the requested resource because another URI exists for the resource corresponding to the request.

303 and 302 Found have the same functionality, but the 303 status code explicitly states that the client should use the GET method to GET the resource.

When the 301, 302, and 303 response status codes return, almost all browsers change POST to GET and remove the body from the request message, after which the request is automatically sent again. Standards 301 and 302 forbid changing the POST method to GET, but everyone does it in practice.

304 Not Modified (The server resource is Not changed and can directly use the client’s unexpired cache)

This status code is fairly common and very important.

Conditional request: The request packet requested by the GET method contains if-match, if-modified-since, if-none-match, if-range, or if-unmodified-since.

  • 304 indicates that when the client sends a request with conditions attached, the server allows the request to access the resource. However, if the request does Not meet the conditions, the server returns 304 Not Modified, indicating that the server resource has Not changed, and the client’s cache that has Not expired can be used directly.
  • The 304 status code returned does not contain any response body.
  • Although 304 is classified as 3XX, it has nothing to do with redirection.

307 Temporary Redirect (Temporary Redirect)

Temporary redirect.

307 has the same meaning as 302Found. Although the 302 standard forbids the conversion of POST to GET, it is not followed in practice.

The 307 does not change from POST to GET in accordance with the browser standards, but each browser may differ in how it behaves when handling the response.

4XX client error (client is the cause of the error)

400 Bad Request

Syntax errors exist in the request packet. Modify the request content and send the request again.

In addition, the browser treats the status code as if it were 200 OK.

401 Unauthorized

Forbidden Access to resources

Indicates that access to the requested resource was denied by the server. (e.g., trying to access from an unauthorized source IP address, which is also very common on many websites)

It is not necessary for the server to give a detailed reason for the rejection; if it does, it can be described in the body of the entity.

404 Not Found

The resource does not exist and cannot be found.

In addition, it can also be used when the server rejects the request and doesn’t want to explain why, you know

5XX Server error (Server error)

500 Internet Server Error

An error occurred while executing the request. Or Internal Web application bugs or temporary faults.

503 Service Unavailable (The server is “not available”)

The server cannot process requests, is temporarily under load or is down for maintenance.

If you know in advance how long it will take to resolve the above situation, it is best to write the retry-after header field and return it to the client.

Actual project use

4XX status code usage practice

In the current use of front-end projects, 4XX errors are generally not directly exposed to the user, and it is recommended to use the 200 status code + code-data-message format for the front and back interfaces. For example:

// HTTP 200 OK
{
    code: 40003,
    data: "error",
    message: "Current user does not have permission to access this resource"
}

// HTTP 200 OK
{
    code: 200,
    data: "I am the resource that requested to be returned.",
    message: "OK"
}
Copy the code

If you are not authorized to log in to a site that requires login permission, return the HTTP 401 Unauthorized status code and add logic to the front-end project to redirect to the login page.

The specific implementation depends on the actual scenario and data privacy requirements of the project.

5XX

In general, a Web service error of type 5XX is a major backend service failure. Front-end students in the implementation of the requirements, should try to avoid the page due to interface return errors and logic stuck, this should be implemented in the specific project logic.