HTTP protocol enhancement
Let's talk about what is the http protocol, the components of the http request message, and the components of the corresponding message; finally understand the common request methods and common response status codes.
What is Communication in the Internet
It is 服务器
to send the content of the communication through the response 客户端浏览器
;
in:
- The main body of the communication is: the server and the client browser
- The content of the communication is: the data transmitted
- The way of communication is: Response
What is the communication protocol in the Internet
To realize the transmission of webpage content between the client and the server, both parties must abide by the transmission agreement of webpage content.
Web content is also called HTTP 超文本
, so the transmission protocol of web content 超文本传输协议
is also called HTTP protocol.
how to communicate
客户端
The data should be passed to in the format required by the HTTP protocol 服务器
, and then
服务器
the data should be responded to in the format required by the HTTP protocol 客户端
.
In other words, HTTP 请求,响应
uses the way of interaction.
HTTP request message components
The http request message is divided into four parts:
- request line
- request header
- Blank line (a horizontal line is displayed on the page, which is used for
分隔请求头部和请求体
) - Request body ( , GET request has no request body)
tips:
只有POST请求才有请求体
Use a picture to understand these four parts:
From the picture, we can see that , 请求行
, 请求头部
, are respectively composed.空行
请求体
How to view http message requests in your own browser
We can right-click to check when the page initiates an ajax request, and view the specific http message request components in NETWORK网络面板
.xhr
Components of the HTTP response message
http响应消息
And http请求消息
the same four parts:
- status line:
- Response header: (describe the basic information of the server)
- Empty line (used to separate the response header and response body)
- Response body (used to store the data content that the server responds to the client)
Use a diagram to describe the four components of the response message:
HTTP request method:
We often use the post
, get
, put
, delete
methods, and of course some other methods, as shown in the figure below:
HTTP response status code
The role of the response status code is to identify the status of the response and let us know how it is responding now.
Each data request will return a response status, which starts with different numbers (eg:, 200
404
etc.), and represents different meanings. Let's talk about the classification of status codes in detail
Response status code classification
Divided into five types: those starting with 1 (basically never encountered), those starting with 2, those starting with 3, those starting with 4, and those starting with 5;
2 at the beginning (representing 成功相关
the response status code)
If we see this, it means the server has successfully received the request and processed it successfully. (there are two cases below)
3 at the beginning (representing 重定向相关
the response status code)
There are three situations:
4 at the beginning (representing 客户端相关
the response status code)
If there is a status code starting with 4, it means that 客户端
there is an error in the request, which will cause the request to fail
5 (server-related response status code)
Indicates that the server failed to correctly process the client's request and an unexpected error occurred.
tips:
The ones starting with 4 are 客户端
errors, and the ones starting with 5 are 服务器端
errors;
tips:
only POST requests have request bodies, and GET requests have no request bodies.