The difference between get and post methods in http requests

One, the principle difference

Generally, when we enter a URL in the browser to visit the website, it is a GET request; in the FORM form, you can specify the submission method as GET or POST by setting the Method. The default is GET submission.

HTTP defines different methods of interacting with the server, of which the most basic four: GET, POST, PUT, DELETE, HEAD, of which GET and HEAD are called safe methods, because HTTP requests using GET and HEAD will not produce any actions . No action means that HTTP requests for GET and HEAD will not produce any results on the server. But the security method is not that no action is generated. The security method here only means that the information will not be modified.

According to the HTTP specification, POST may modify the request for resources on the server. For example, Zhihu writes an article, a user submits an article or a reader submits a comment through a POST request, because the resource (ie a certain page) is different after the article is submitted or the comment is submitted, or the resource has been modified. It is the "unsafe method".

Two, the most intuitive difference when using

The most intuitive difference is that GET includes the parameters in the URL, and POST passes the parameters through the request body.

get request:
The difference between get and post methods in http requests

post request:
The difference between get and post methods in http requests

Three, why get is faster than post

1. The post request contains more request headers

Because the post needs to contain data in the body part of the request, there will be a few more header fields (such as content-type) in the data description part, which is actually negligible.

2. The most important one, the post will send the request header to the server for confirmation before actually receiving the data, and then sending the data

The process of post request:
(1) The browser requests a tcp connection (first handshake)
(2) The server agrees to make a tcp connection (second handshake)
(3) The browser confirms and sends the post request header (third handshake) , This message is relatively small, so http will send data for the first time at this time)
(4) The server returns a 100 Continue response
(5) The browser sends data
(6) The server returns a 200 OK response

The process of get request:
(1) The browser requests a tcp connection (first handshake)
(2) The server agrees to make a tcp connection (second handshake)
(3) The browser confirms and sends the get request header and data (third The second handshake, this message is relatively small, so http will send the first data at this time)
(4) The server returns a 200 OK response.
That is to say, the total consumption of the visual get is about 2/3 of the post, this mouth says Without proof, some netizens have tested it online.

3. get will cache the data, but post will not

You can do a short test. When using ajax to request static data (such as html pages, pictures) in the get method, if the data transmitted twice is the same, the time consumed after the second time will be within 10ms (chrome test). The time consumed by each post is almost the same. After testing, under Chrome and Firefox, if it is detected that the get request is a static resource, it will be cached. If it is data, it will not be cached, but IE will cache everything. Of course, no one should use post to get static data. I haven't seen it anyway.

Fourth, the interview is generally how to answer the difference between get and post

(1) Post is more secure (will not be part of the url, will not be cached, stored in the server log, and browser browsing history)

(2) The data sent by post is larger (get has URL length limitation)

(3) Post can send more data types (get can only send ASCII characters)

(4) Post is slower than get

(5) Post is used to modify and write data, get is generally used for searching, sorting and filtering operations (Taobao, Alipay search queries are all get submitted), the purpose is to obtain resources, read data

5. Tools for testing get and post requests

Get and post requests generally use interface test tools. I personally use interface test tools: apipost and jmeter.

Apipost is a domestic interface testing tool and interface document generation tool that can meet our daily work requirements for interface testing and interface document generation. Interface documents can also support the generation of multiple formats. There are interface documents in multiple formats such as online version, markdown, and word version.

The difference between get and post methods in http requests

Jmeter can perform interface testing and performance testing, but for simple interface testing, jmeter is not as easy to use as apipost. Jmeter focuses on stress testing, stability testing and load testing. An interface testing tool designed for the stability of interfaces and programs, with software performance as the main interface test as a supplement.

The difference between get and post methods in http requests

Tool download address:https://www.apipost.cn/

Guess you like

Origin blog.51cto.com/12246704/2547286