Interpretation of HTTP protocol status codes: analysis of common 2xx, 3xx, 4xx and 5xx status codes, specific code examples are required
HTTP protocol is a protocol used for network transmission An application layer protocol that defines the rules for communication between clients and servers. During the process of HTTP request and response, the server will return a status code to indicate the processing result of the request. Status codes are divided into five categories, namely 1xx, 2xx, 3xx, 4xx and 5xx. This article will focus on the common 2xx, 3xx, 4xx and 5xx status codes and provide specific code examples.
200 OK: Indicates that the request is successful and the requested resource is returned. For example, a 200 status code is returned when retrieving the content of a web page.
204 No Content: Indicates that the request was successful, but the server did not return any content. For example, when performing a delete operation on a resource, the server can return a 204 status code.
206 Partial Content: Indicates that the server successfully processed part of the request and returned partial content. For example, when requesting a range of files through the HTTP Range header field, the server may return a 206 status code.
The following is a sample code that returns a 206 status code:
HTTP/1.1 206 Partial Content Content-Length: 10000 Content-Range: bytes 0-9999/20000 Content-Type: video/mp4 [视频的字节范围]
301 Moved Permanently: Indicates that the requested resource has been permanently moved to a new location. For example, a web page is permanently redirected to another URL.
302 Found: Indicates that the requested resource has been temporarily moved to a new location. For example, a webpage temporarily redirects to another URL.
307 Temporary Redirect: Indicates that the requested resource has been temporarily moved to a new location, and the client should continue to use the original URL to send requests. The difference with the 302 status code is that the client should keep the original request's HTTP method unchanged and resend it.
The following is a sample code that returns a 301 status code:
HTTP/1.1 301 Moved Permanently Location: https://www.new-url.com/
400 Bad Request: Indicates that the request sent by the client has a syntax error. For example, a required parameter is missing from the request.
401 Unauthorized: Indicates that the request requires authentication, but the client did not provide valid credentials. For example, a 401 status code is returned when trying to access a page that requires login.
404 Not Found: Indicates that the requested resource was not found. For example, the client requested a URL that does not exist.
The following is a sample code that returns a 404 status code:
HTTP/1.1 404 Not Found Content-Type: text/html <!DOCTYPE html> <html> <head> <title>404 Not Found</title> </head> <body> <h1>Not Found</h1> <p>The requested URL was not found on this server.</p> </body> </html>
500 Internal Server Error: Indicates that an internal error occurred while the server was processing the request. For example, server configuration errors or code logic errors.
503 Service Unavailable: Indicates that the server is temporarily unable to process the request. This is usually caused by server overload or maintenance. For example, when the website is undergoing an upgrade, a 503 status code can be returned.
The following is a sample code that returns a 500 status code:
HTTP/1.1 500 Internal Server Error Content-Type: text/html <!DOCTYPE html> <html> <head> <title>500 Internal Server Error</title> </head> <body> <h1>Internal Server Error</h1> <p>An unexpected condition was encountered while processing the request.</p> </body> </html>
By understanding the meaning of common 2xx, 3xx, 4xx and 5xx status codes and mastering the corresponding code examples, you can better To cope with various situations that may be encountered during HTTP requests. This enables more efficient development and debugging, and improves user experience.
The above is the detailed content of Parsing and analysis of different types of HTTP status codes: common 2xx, 3xx, 4xx and 5xx status codes. For more information, please follow other related articles on the PHP Chinese website!