HTTP Response Status Code Encyclopedia: From 100 to 511, comprehensively analyze various situations of HTTP requests


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.

insert image description here

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:
  • GET: The resource has been fetched and transferred in the message body
  • HEAD: The entity header is in the message body
  • POST: A resource describing the result of the action is transmitted in the message body
  • TRACE: The message body contains the request message received by the server
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
  • The server has successfully processed the request, but the entity header meta-information returned is not a definite set valid on the original server, but a copy from the local or a third party. The current information may be a subset or a superset of the original version.
  • Using this status code is not required, and is only appropriate if the response would return 200 OK without this status code
204 No Content
  • The server successfully processed the request, but does not need to return any entity content, and expects to return updated meta information . The response may return new or updated meta information in the form of entity headers
  • These headers, if present, should correspond to the requested variables. If the client is a browser, then the user's browser should retain the page that sent the request without any change in the document view, even though new or updated meta information should be applied to the user's browser activity according to the specification Documentation in view
  • Since the 204 response is forbidden to contain any message body, it always ends with the first empty line after the message header
205 Reset Content
  • The server successfully processed the request and returned nothing. But unlike a 204 response, a response that returns this status code requires the requester to reset the document view . This request is mainly used to reset the form immediately after accepting user input, so that the user can easily start another input
  • Like the 204 response, this response is also forbidden to contain any message body and ends with the first blank line after the message header
206 Partial Content
  • The server has successfully processed some GET requests . HTTP download tools like FlashGet or Xunlei use this type of response to resume downloads or break down a large file into multiple download segments to download simultaneously
  • The request must contain Range header information to indicate the content range that the client expects , and may contain If-Range as a request condition
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
  • The requested resource has been permanently moved to a new location , and any future references to this resource should use one of the several URLs returned by this response. If possible, clients with link editing capabilities should automatically modify the requested address to the address returned from the server
  • Unless otherwise specified, this response is also cacheable
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
  • 这意味着资源现在永久位于 Location:HTTP Response 标头指定的另一个 URL
  • 这与301 Moved Permanently HTTP 响应代码具有相同的语义,但用户代理不能更改所使用的 HTTP 方法:如果第一个请求中使用 POST,则必须在第二个请求中使用 POST

五、4xx 客户端响应

1. 认识http客户端响应

  1. HTTP客户端是指发起HTTP请求的客户端应用程序或设备。当HTTP客户端发送请求到服务器端时,服务器会返回HTTP响应。HTTP客户端会负责接收和处理这个HTTP响应
  2. HTTP客户端响应是指服务器返回给客户端的HTTP响应报文

2. 常见的客户端响应状态码

400 Bad Request **
  • 语义有误,当前请求无法被服务器理解。除非进行修改,否则客户端不应该重复提交这个请求
  • 请求参数有误
401 Unauthorized
  • 当前请求需要用户验证。该响应必须包含一个适用于被请求资源的 WWW-Authenticate 信息头用以询问用户信息
  • 客户端可以重复提交一个包含恰当的 Authenticate 头信息的请求。如果当前请求已经包含了 Authenticate 证书,那么 401 响应代表着服务器验证已经拒绝了那些证书。如果 401 响应包含了与前一个响应相同的身份验证询问,且浏览器已经至少尝试了一次验证,那么浏览器应当向用户展示响应中包含的实体信息,因为这个实体信息中可能包含了相关诊断信息
402 Payment Required
此响应码保留以便将来使用,创造此响应码的最初目的是用于数字支付系统
403 Forbidden
  • 服务器已经理解请求,但是拒绝执行它。与 401 响应不同的是,身份验证并不能提供任何帮助,而且这个请求也不应该被重复提交。如果这不是一个 HEAD 请求,而且服务器希望能够讲清楚为何请求不能被执行,那么就应该在实体内描述拒绝的原因。
  • 当然服务器也可以返回一个 404 响应,假如他不希望客户端获得任何信息
404 Not Found
  • 请求失败,请求所希望得到的资源未被在服务器上发现。没有信息能够告诉用户这个状况到底是暂时的还是永久的。假如服务器知道情况的话,应当使用 410 状态码来告知旧资源因为某些内部配置机制问题,已经永久的不可用,而且没有任何可以跳转的地址
  • 404 这个状态码被广泛应用于当服务器不想揭示到底为何请求被拒绝或者没有其他适合的响应可用的情况下
405 Method Not Allowed
  • 请求行中指定的请求方法不能被用于请求相应的资源。该响应必须返回一个 Allow 头信息用以表示出当前资源能够接受的请求方法的列表
  • 鉴于 PUT,DELETE 方法会对服务器上的资源进行写操作,因而绝大部分的网页服务器都不支持或者在默认配置下不允许上述请求方法,对于此类请求均会返回 405 错误
406 Not Acceptable
请求的资源的内容特性无法满足请求头中的条件,因而无法生成响应实体
407 Proxy Authentication Required
与 401响应相似,只不过客户端必须在代理服务器上进行身份验证。代理服务器必须返回一个 Proxy-Authenticate 用以进行身份询问。客户端可以返回一个 Proxy-Authorization 信息头用以验证
408 Request Timeout
请求超时。客户端没有在服务器预备等待的时间内完成一个请求的发送。客户端可以随时再次提交这个请求而无需进行任何更改
409 Conflict
由于和被请求的资源的当前状态之间存在冲突,请求无法完成。这个代码只允许用在这样的情况才能被使用:用户被认为能够解决冲突,并且会重新提交新的请求。该响应应当包含足够的信息以便用户发现冲突的源头
410 Gone
  • 被请求的资源在服务器上已经不再可用,而且没有任何已知的转发地址。这样的状况应当被认为是永久性的。如果可能,拥有链接编辑功能的客户端应当在获得用户许可后删除所有指向这个地址的引用。如果服务器不知道或者无法确定这个状况是否为永久的,那么就应该使用 404 状态码
  • 除非额外说明,否则这个响应是可缓存的
411 Length Required
服务器拒绝在没有定义 Content-Length 头的情况下接受请求。在添加了标明请求消息体长度的有效 Content-Length 头后,客户端可以再次提交该请求
412 Precondition Failed
<font size=“3” >服务器在验证在请求的头字段中给出先决条件时,没能满足其中的一个或者多个。这个状态码允许客户端在获取资源的请求的元信息(请求头字段数据)中设置先决条件,以此来避免该请求方法被应用到其希望的内容以外的资源上
413 Payload Too Large
  • 服务器拒绝处理当前请求,因为该请求提交的实体数据大小超过了服务器愿意或者能够处理的范围。此种情况下,服务器可以关闭连接以免客户端继续发送此请求
  • 如果这种状况是临时的,服务器应当返回一个 Refty-After 的响应头,以告知客户端可以在多少时间以后重新尝试
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
  • 服务器没有准备好处理请求。常见原因是服务器因维护或重载而停机。请注意,与此响应一起,应发送解释问题的用户友好页面。这个响应应该用于临时条件和 Retry-After:如果可能的话,HTTP 头应该包含恢复服务之前的估计时间
  • 网站管理员还必须注意与此响应一起发送的与缓存相关的标头,因为这些临时条件响应通常不应被缓存
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)

Guess you like

Origin blog.csdn.net/HHX_01/article/details/132345771