The responsibility of the status code is to describe the response to the request when the client sends a request to the server.
With the help of status codes, users can know whether the server processed the request normally or whether an error occurred.
The status code defined according to the Http/1.1 protocol RFC2616 consists of 3 digits and a reason message.
The first digit in the number specifies the response category, the last two digits are uncategorized. There are 5 response categories:
| Type | Reason-phrase | Note |
|---|---|---|
| 1XX | Informational | Informational status code indicating that the accepted request is being processed |
| 2XX | Success | Success status code, indicating that the request has been processed normally |
| 3XX | Redirection | Redirection status code, indicating that the client needs to perform additional operations |
| 4XX | Client Error | Client error status code, indicating that the server cannot process the request |
| 5XX | Server Error | Server error status code, indicating that the server has an error in processing the request |
There are 37 types of HTTP status codes recorded in RFC2616. Together with “WebDAV” (RFC4918, 5842) and “Additional HTTP Status Codes” (RFC6585), the number reaches more than 60 types.
However, there are so many HTTP status codes. In fact, there are only about 14 commonly used status codes. This article will talk about these 14 status codes.
2XX Success
This class of status code indicates that the client’s request was successfully received, understood, and accepted.
The 2xx response result indicates that the request sent from the client was processed normally on the server side.
200 OK
The request is successfully processed, and the server will return results according to different request methods: GET: The corresponding resource requested will be returned as a response. HEAD: The response header (entity-header) of the requested resource will be returned as the response, excluding the response body (message-body). POST: Returns the result of processing the corresponding request.
204 No Content
This status code indicates that the request received by the server has been processed, but the server does not need to return a response body. For example, if the client is a browser and the request returned returns a 204 response, then the page displayed by the browser will not be updated.
206 Partial Content
This status code indicates that the client made a range request and the server successfully executed this part of the GET request.
Requests initiated by the client must include the Range field in the request header. The server response message must contain the entity content (entity-bodies) in the range specified by Content-Range
3XX Redirection
This class of status code indicates that further action needs to be taken by the user agent in order to fulfill the request. A 3XX response indicates that the browser needs to perform some special processing to complete the request.
301 Movied Permanently
Permanent redirect. This status code indicates that the requested resource has been assigned a new URI and that the URI to which the resource now refers will be used in the future. And there are different processing methods according to the request method:
HEAD: The new permanent URI must be specified in the response header Location field.
GET: In addition to the Location field, the hyperlink text of the permanent URI needs to be attached in the response body.
POST: After sending a POST request and receiving a 301 response, the client should not automatically jump to the URI, but should ask the user to confirm the jump.
For example, if a URI has been bookmarked in the browser, it should be re-saved according to the URI prompted by the Location header field.
302 Found
Temporary redirection. This status code indicates that the requested resource has been assigned a new URI, and it is hoped that the user can use the new URI to access it this time. It is similar to the 301 Moved Permanently status code, but the resource represented by the 302 status code is not permanently moved, but is only temporary. If the user bookmarks a URI, the 302 response will not update the bookmark like the 301 response.
303 See Other
This status code indicates that because there is another URI for the resource corresponding to the request, the GET method should be used to obtain the requested resource. The difference between 303 and 302 is that 302 does not change the request method. If the request method is POST, the redirected request should also be POST. For 303, if you use POST request, the redirected request should be GET request.
But one thing to note is that many browsers before HTTP/1.1 cannot correctly understand the 303 status code. Many existing browsers treat 302 responses as 303 responses, and use the GET method to access the URI specified in Location, regardless of the original request method.
There is a relevant original text in RFC2616:
Note: Many pre-Http/1.1 user agents do not understand the 303 status. When interoperability with such clients is a concern, the 302 status code may be used instead, since most user agents react to a 302 response as described here for 303.
304 Not Modified
This status code indicates that when the client sends a request with conditions, the server allows the request to access the resource, but the conditions are not met. A 304 status code is returned without any subject part of the response. A conditional request refers to a request header using the GET method that contains any of the following headers: If-Match, If-Modified-Since, If-None-Match, If-Range, If-Unmodified-Since.
307 Temporary Redirect
Temporary redirection. This status code has a similar meaning to 302 and 303. The difference is that the 307 status code does not specify what request method the client should use to request the redirect address. (302 specifies using the original request method, 303 specifies using the GET method)
4XX Client Error
The 4xx class of status code is intended for cases in which the client seems to have erred.
4XX responses indicate that the client is the cause of the error
400 Bad Request
Indicates that there is a syntax error in the request message, causing the server to be unable to understand the request. The client needs to modify the content of the request and send the request again.
401 Unauthorized
This status code indicates that the request sent needs to have authentication information that passes HTTP authentication (Basic authentication, Digest authentication). A response containing 401 must be returned and must contain WWW-Authenticate in the header to indicate which type of authentication the server requires. When the client requests the resource again, it needs to include authentication information in Authorization in the request header. For more information about authentication and authorization, please follow RFC2617
403 Forbidden
This status code indicates that access to the requested resource was denied by the server. The server does not need to give detailed reasons for rejection, but if it wants to explain, it can describe the reasons in the main body of the entity so that users can see it. A 403 response may occur if access permission to the file system is not obtained, there is some problem with the access permission, or access is attempted from an unauthorized source IP address.
404 Not Found
This status code indicates that the specified resource cannot be found on the server. Usually used when the server does not want to disclose the reason for rejecting the request, or there is no other response to provide.
5XX Server Error
Response status codes beginning with the digit “5” indicate cases in which the server is aware that it has erred or is incapable of performing the request. A 5XX response indicates that the server itself has an error or does not have sufficient capacity to handle the request.
500 Internal Server Error
This status code indicates that an error occurred on the server side while executing the request. It may also be a bug in the web application or some temporary failure.
503 Service Unavailable
This status code indicates that the server is temporarily overloaded or is undergoing downtime for maintenance and is currently unable to process requests. If you know the time required to release the above in advance, it is best to write the Retry-After header field and return it to the client.