The difference between GET and POST in HTML HTTP

99% of people understand the difference between GET and POST in HTTP (transfer)



Transfer from: WebTechGarden WeChat public account

GET and POST are two basic methods of HTTP request, to say the difference between them, anyone who has contacted WEB development can tell one or two.

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

You may have written countless GET and POST requests yourself, or have seen the differences summarized by many authoritative websites. You know exactly when to use.

When you are asked this question during an interview, your heart is full of confidence and joy.
 

072536_U5BK_2896879.png
 

You easily gave a "standard answer":
  • GET is harmless when the browser rolls back, and POST will submit the request again.
  • The URL address generated by GET can be Bookmark, but POST cannot.
  • GET requests will be actively cached by the browser, but POST will not, unless manually set.
  • GET requests can only be URL encoded, while POST supports multiple encoding methods.
  • The GET request parameters will be completely retained in the browser history, while the parameters in POST will not be retained.
  • The parameters transmitted by the GET request in the URL are limited in length, but not in the POST.
  • For the data type of the parameter, GET only accepts ASCII characters, and POST has no restrictions.
  • GET is less secure than POST, because the parameters are directly exposed on the URL, so they cannot be used to pass sensitive information.
  • The GET parameter is passed through the URL, and the POST is placed in the Request body.



(The answer to this standard is from w3schools)

"Unfortunately, this is not the answer we want!"
 

072621_vmDd_2896879.png



Please tell me the truth. . .

If I tell you that GET and POST are essentially the same, do you believe it?

Let's take off the coats of GET and POST and meet frankly!

What are GET and POST? Two methods of sending requests in the HTTP protocol.

What is HTTP?

HTTP is a protocol based on TCP / IP on how data is communicated in the World Wide Web.

The bottom layer of HTTP is TCP / IP, so the bottom layer of GET and POST is also TCP / IP, that is, both GET / POST are TCP links.

What GET and POST can do is the same. You need to add request body to GET and bring url parameter to POST, which is completely technically feasible.

So, what are the differences in the "standard answers"?
 



In my big world of the World Wide Web, TCP is like a car. We use TCP to transport data. It is very reliable, and there will never be a phenomenon of missing or missing parts.

But if all the cars running on the road look exactly the same, then the world looks like a mess. The cars sending urgent items may be blocked on the road by the cars full of goods in front, and the entire transportation system will be paralyzed.

To avoid this, traffic rules HTTP was born.

HTTP sets several service categories for automobile transportation, including GET, POST, PUT, DELETE and so on.

HTTP stipulates that when performing a GET request, the car should be labeled with GET (set method to GET), and it is required to put the transmitted data on the roof of the car (in the url) for easy recording.

If it is a POST request, it is necessary to put a POST label on the car and put the goods in the car.

Of course, you can also secretly hide some goods in the car during GET, but this is very disgraceful; you can also put some data on the roof of the car during POST, which makes people feel silly.

HTTP is just a code of conduct, and TCP is the basis of how GET and POST are implemented.

However, we only see HTTP requests for the transmission channel of GET and POST parameters (url or requrest body).

Where does the limit on the parameter size in the "standard answer" come from?
 



In my world of the World Wide Web, there is another important role: the transportation company.

Different browsers (initiating http requests) and servers (accepting http requests) are different transport companies.

Although in theory, you can stack unlimited cargo on the roof (infinitely add parameters in url).

However, transportation companies are not stupid. Loading and unloading are also very costly. They will limit the single transportation volume to control the risk. Too much data is a great burden on browsers and servers. The unwritten rule in the industry is that (most) browsers usually limit url length to 2K bytes, while (most) servers handle urls up to 64K in size. The excess will not be processed.

If you use the GET service and secretly hide the data in the request body, different servers process it differently. Some servers will help you unload and read the data, and some servers ignore it directly, so although GET can bring the request body, it ca n’t Guaranteed to be received.

Well, now you know that GET and POST are essentially TCP connections, no difference.

However, due to HTTP regulations and browser / server restrictions, they have shown some differences in the application process.

Do you think this article is over?


Our big boss is still waiting to play. . .

How mysterious is this boss?

When you try to find "the difference between GET and POST" on the Internet, those search results you will see never mention him.

What is he? . .

There is another major difference between GET and POST. Simply put: GET generates one TCP packet; POST generates two TCP packets.

Long saying: for GET requests, the browser sends the http header and data together, and the server responds with 200 (return data); for POST, the browser sends the header first, and the server responds with 100 (continue), the browser Send data again, the server responds with 200 (return data).

That is to say, GET only needs one car run to deliver the goods, and POST has to run two times. The first time, go to the server to say hello: "Hi, I will send a batch of goods in a minute Open the door to greet me, "and then return to deliver the goods.

Because POST requires two steps and consumes a little more time, it seems that GET is more effective than POST.

Therefore, the Yahoo team recommends replacing POST with GET to optimize website performance.

But this is a pit! ! ! Be cautious when jumping in.

why?
 

  • GET and POST have their own semantics and cannot be mixed together.
  • According to research, under a good network environment, the difference between the time to send one packet and the time to send two packets can be basically ignored. When the network environment is poor, the TCP of two packets has a great advantage in verifying the integrity of the data packet.
  • Not all browsers send packets twice in POST, for example Firefox only sends them once.



Now, when the interviewer asks you again "the difference between GET and POST", is your heart like this?
 

Published 105 original articles · won praise 4 · Views 5141

Guess you like

Origin blog.csdn.net/LBJ8888888/article/details/105354569