Article Directory
foreword
In order to consolidate the knowledge learned, the author tried to start publishing some blogs of learning notes for future review. Of course, it would be great if it could help some newcomers learn new technologies. The author is a piece of food. If there are any mistakes in the records in the article, readers and friends are welcome to criticize and correct.
(The reference source code of the blog can be found in the resources on my homepage. If you have any questions during the learning process, please feel free to ask me in the comment area)
First, understand the response status code
1. What is HTTP response status code
HTTP status codes are three-digit codes returned by servers in response to client requests. They are used to represent the processing status and results of HTTP requests. Each status code has a specific meaning and is used to convey information to the client about the processing of the request.
2. The role of Http response status code
-
Provide request processing result information : The HTTP status code informs the client of the processing status of the request, including success, redirection, client error, and server error. Through the status code, the client can know whether the request was successfully processed and how to further process the response data.
-
Identify the cause of the request error : The status code can help the client locate the cause of the request error. For example, when the client sends an invalid request or the requested resource does not exist, the server will return a corresponding error status code, so that the client can understand the specific cause of the error and take appropriate measures.
-
Support protocol extension and upgrade : The range of HTTP status codes leaves a certain space to support future protocol extensions and upgrades. By defining new status codes, it is possible to provide accurate representations for new protocol functions or processing situations.
-
For debugging and troubleshooting : Status codes play an important role in the debugging and troubleshooting process. By viewing the status codes, developers can track the progress of request processing and identify the specific link where the problem occurred so that they can be fixed and improved.
-
Helps build a good user experience : The correct use of status codes contributes to a good user experience. For example, reasonable use of redirection status codes can guide users to the correct page, while accurate error status codes can provide users with friendly error prompts and improve user satisfaction.
3. Suggestions for optimizing and debugging HTTP requests
-
Use appropriate HTTP method : Choose the correct HTTP method to match the purpose of the request. Common methods include GET, POST, PUT, DELETE, etc. Ensure that the most appropriate method is used to perform a specific operation to increase efficiency and security.
-
Reduce the number of requests : Reducing the number of unnecessary requests can significantly improve performance. Consider using techniques such as request coalescing, caching, and batch operations to reduce the number of requests and minimize server and network load.
-
Minimize request size : Reducing the size of requests can reduce network transmission costs and request processing time. Optimize the size of the request, such as reducing the size of the request by compressing, reducing redundant data, and merging requests.
-
Use HTTP caching : Utilizing HTTP caching can reduce requests to the server. Reasonably set cache headers, including Cache-Control, Expires, etc., and mechanisms to verify whether the cache is still valid, such as ETag and Last-Modified.
-
Compress response data : Compress the response data using a compression algorithm such as Gzip to reduce the size of the response. The client can indicate support for compressed responses by setting the Accept-Encoding header, and the server can indicate the compression method of the response data by setting the Content-Encoding header.
-
Use an appropriate data format : Choose a format suitable for data exchange, such as JSON, XML, etc. Select the appropriate data format according to requirements and scenarios to improve the efficiency and ease of data transmission.
-
Monitoring and logging : Use appropriate tools and techniques to monitor and log information about HTTP requests and responses. By recording logs and analyzing monitoring data, you can help discover potential performance problems and errors, as well as debug and optimize.
-
Use appropriate tools and libraries : Using excellent HTTP request libraries or frameworks, such as cURL, HttpClient, etc., can simplify the development process and provide more efficient request processing and debugging functions.
-
Conduct performance testing : Conduct performance testing on HTTP requests to evaluate system performance and identify bottlenecks. Use tools such as Apache JMeter, LoadRunner, etc. for load testing and stress testing to identify performance bottlenecks and optimize them.
-
Carefully analyze error messages : For errors that occur, carefully analyze error messages and status codes to understand the root cause of the problem. Combined with logs and other debugging tools, trace the processing process of the request, find out where the error is and take corresponding repair measures.
2. 1xx information response
1. Recognize http information response
HTTP information response refers to the response message returned by the server to the client after the client sends an HTTP request to the server
2. Common message response status codes
100 Continue |
This provisional response indicates that everything so far is available and the client should continue with the request, ignoring it if already done
|
101 Switching Protocol |
This code is sent in response to the client's Upgrade header and indicates that the server is also switching protocols
|
102 Processing(WebDAV) |
This code means that the server has received and is processing the request, but no response is available
|
103 Early Hints |
This status code is primarily intended for use with the Link header to allow user agents to start preloading resources while the server is still preparing a response
|
3. 2xx successful response
1. Recognize HTTP success response
A successful HTTP status code response means that the server has successfully processed the client's request and returned an expected response
2. Common successful response status codes
200 OK |
The request was successful, the meaning of success depends on the HTTP method:
|
201 Created |
The request was successful and a new resource was created . This is usually the response returned after a POST request, or some PUT request
|
202 Accepted |
The request has been received, but not yet responded to, with no results . Means that there will be no asynchronous response to indicate the result of the current request, expecting another process or service to process the request, or batch processing
|
203 Non-Authoritative Information |
|
204 No Content |
|
205 Reset Content |
|
206 Partial Content |
|
207 Multi-Status (WebDAV) |
The status code extended by WebDAV (RFC 2518), which means that the subsequent message body will be an XML message , and may contain a series of independent response codes according to the number of previous sub-requests
|
208 Already Reported(WebDAV) |
In DAV use: propstat response element to avoid repeated enumeration of internal members of multiple bindings to the same collection
|
226 IM Used(HTTP Delta encoding) |
The server has completed a GET request for a resource, and the response is a representation of the results of one or more instance operations applied to the current instance
|
4. 3xx redirection
1. Understanding http redirection
HTTP status code redirection is to return a specific status code after the server receives the client's request, indicating that the client needs to take further actions to complete the request
2. Common redirect status codes
300 Multiple Choice |
The resource being requested has a range of optional return information , each with its own specific address and browser-driven negotiation information. The user or browser can choose a preferred address for redirection
|
301 Moved Permanently |
|
302 Found |
The requested resource is now temporarily responding to requests from a different URL . Since such redirection is temporary, the client SHOULD continue to send future requests to the original address . This response is cacheable only if specified in Cache-Control or Expires
|
303 See Other |
The response corresponding to the current one can be found at another URL, and the client should GET to that resource . This method exists primarily to allow the output of a script-activated POST request to be redirected to a new resource
|
304 Not Modified |
The server SHOULD return this status code if the client sends a conditional GET request and the request is granted, but the content of the document has not changed (since the last access or according to the requested conditions) . 304 responses are forbidden to contain a message body, so always end with the first empty line of the message header
|
305 Use Proxy |
被请求的资源必须通过指定的代理才能访问。Location 域中将给出指定的代理所在的 URL 信息,接受这需要重复发送一个单独的请求,通过这个代理才能访问相应资源。只有原始服务器才能建立 305 响应
|
306 unused |
在最新版的规范中,306 状态码已经不再被使用
|
307 Temporary Redirect |
请求的资源现在临时从不同的 URL 响应请求。由于这样的重定向是临时的,客户端应当继续向原有地址发送以后的请求。只有在 Cache-Control 或 Expires 中进行了指定的情况下,这个响应才是可缓存的
|
308 Permanent Redirect |
|
五、4xx 客户端响应
1. 认识http客户端响应
- HTTP客户端是指发起HTTP请求的客户端应用程序或设备。当HTTP客户端发送请求到服务器端时,服务器会返回HTTP响应。HTTP客户端会负责接收和处理这个HTTP响应
- HTTP客户端响应是指服务器返回给客户端的HTTP响应报文
2. 常见的客户端响应状态码
400 Bad Request | **
|
401 Unauthorized |
|
402 Payment Required |
此响应码保留以便将来使用,创造此响应码的最初目的是用于数字支付系统
|
403 Forbidden |
|
404 Not Found |
|
405 Method Not Allowed |
|
406 Not Acceptable |
请求的资源的内容特性无法满足请求头中的条件,因而无法生成响应实体
|
407 Proxy Authentication Required |
与 401响应相似,只不过客户端必须在代理服务器上进行身份验证。代理服务器必须返回一个 Proxy-Authenticate 用以进行身份询问。客户端可以返回一个 Proxy-Authorization 信息头用以验证
|
408 Request Timeout |
请求超时。客户端没有在服务器预备等待的时间内完成一个请求的发送。客户端可以随时再次提交这个请求而无需进行任何更改
|
409 Conflict |
由于和被请求的资源的当前状态之间存在冲突,请求无法完成。这个代码只允许用在这样的情况才能被使用:用户被认为能够解决冲突,并且会重新提交新的请求。该响应应当包含足够的信息以便用户发现冲突的源头
|
410 Gone |
|
411 Length Required |
服务器拒绝在没有定义 Content-Length 头的情况下接受请求。在添加了标明请求消息体长度的有效 Content-Length 头后,客户端可以再次提交该请求
|
412 Precondition Failed |
<font size=“3”
>服务器在验证在请求的头字段中给出先决条件时,没能满足其中的一个或者多个。这个状态码允许客户端在获取资源的请求的元信息(请求头字段数据)中设置先决条件,以此来避免该请求方法被应用到其希望的内容以外的资源上
|
413 Payload Too Large |
|
414 URL Too Long |
请求的 URL 长度超过了服务器能够解释的长度,因此服务器拒绝对该请求提供服务。这比较少见,通常情况包括:本应使用 POST 方法的表单提交变成了 GET 方法,导致查询字符串(Query String)过长
|
415 Unsupported Media Type |
对于当前请求的方法和所请求的资源,请求中提交的实体并不是服务器所支持的格式,因此请求被拒绝
|
416 Range Not Satisfiable |
如果请求中包含了 Range 请求头,并且 Range 中指定的任何数据范围都与当前资源的可用范围不重合,同时请求中又没有定义 If-Range 请求头,那么服务器就应当返回 416 状态码
|
417 Expectation Failed |
此响应代码意味着服务器无法满足 Expect 请求标头字段指示的期望值
|
418 I’m a teapot |
服务器拒绝尝试用“茶壶冲泡咖啡”(愚人节玩笑)
|
421 Misdirected Request |
该请求针对的是无法产生响应的服务器。这可以由服务器发送,该服务器为配置为针对包含在请求 URL 中的方案和权限的组合产生响应
|
422 Unprocessable Entity(WebDAV) |
请求格式良好,但由于语义错误而无法遵循
|
423 Locked(WebDAV) |
正在访问的资源被锁定
|
424 Failed Dependency(WebDAV) |
由于先前的请求失败,所以这次请求失败
|
425 Too Early |
服务器不愿意冒着风险去处理可能重播的请求
|
426 Upgrade Required |
服务器拒绝使用当前协议执行请求,但可能在客户机升级到其他协议后愿意这样做。服务器在 426 响应中发送 Upgrade 头一直是所需的协议
|
428 Precondition Required |
原始服务器要求该请求是有条件的。旨在防止“丢失更新”问题,即客户端获取资源状态,修改改状态并将其返回服务器,同时第三方修改服务器上的状态,从而导致冲突
|
429 Too Many Requests |
用户在给定时间内发送了太多请求(“限制请求速率”)
|
431 Request Header Fields Too Large |
服务器不愿意处理请求,因为他的请求头字段太大。请求可以在减少请求头字段的大小后重新提交
|
451 Unavailable For Legal Reasons |
用户请求非法资源,例如:由政府审查的网页
|
六、5xx 服务端响应
1. 认识HTTP服务端响应
HTTP服务端响应是指服务器对客户端的HTTP请求做出的响应。服务器接收到客户端的请求后,会根据请求的内容和服务器端的处理逻辑生成一个HTTP响应,然后将该响应发送回客户端
2. 常见的服务端响应状态码
500 Internal Server Error |
服务器遇到了不知道如何处理的情况
|
501 Not Implemented |
此请求方法不被服务器支持且无法被处理。只有 GET 和 HEAD 时要求服务器支持的,他们必定不会返回次错误代码
|
502 Bad Gateway |
此错误响应表明服务器作为网关需要得到一个处理这个请求的响应,但是得到一个错误的响应
|
503 Service Unavailable |
|
504 Gateway Timeout |
当服务器作为网关,不能及时得到响应时返回此错误代码
|
505 HTTP Version Not Supported |
服务器不支持请求中所使用的 HTTP 协议版本
|
506 Variant Also Negotiates |
服务器有一个内部配置错误:对请求的透明内容协议导致循环引用
|
507 Insufficient Storage |
服务器有内部配置错误:所选的变体资源被配置为参与透明内容协商本身,因此不是协商过程中的适当端点
|
508 Loop Detected(WebDAV) |
服务器在处理请求时检测到无限循环
|
510 Not Extended |
The client needs to further extend the request for the server to fulfill it . The server replies with all the information the client needs to make the extension request
|
511 Network Authentication Required |
The 511 status code indicates that the client needs to authenticate to gain network access
|
Summarize
Everyone is welcome to leave a message for exchange and criticism. If the article is helpful to you or you think the author's writing is not bad, you can click to follow, like, and bookmark to support.
(The reference source code of the blog can be found in the resources on my homepage. If you have any questions during the learning process, please feel free to ask me in the comment area)