This is the fifth day of my participation in the August More text Challenge. For details, see: August More Text Challenge

Recently, I have been doing a series of summary questions on the front end, interested friends can add attention, welcome correction, communication.

Strive for each knowledge point to be able to sum up some more, at least to do in the interview, for each knowledge point can be kan, not dumb fire.

preface

In the process of front and back interaction, the two methods that are most frequently used are GET and POST, and they are also the knowledge points that are frequently asked about in interviews.

A common interview question is: What’s the difference between the GET and POST methods?

This question often leads us to focus only on the differences between the two and ignore the commonalities, so the answer to the question is a little shallow.

Today we’ll take a closer look at the similarities and differences between the two methods.

The same

How many people would disagree if I said GET and POST are essentially the same?

The GET and POST methods are just two kinds of request methods specified by the HTTP protocol for different division of labor.

What is HTTP? HTTP is a TCP/ IP-based protocol for how data is communicated in the World Wide Web.

The underlying layer of HTTP is TCP/IP. So the underlying layer of GET and POST is ALSO TCP/IP, which means that both GET and POST are TCP links. So they’re essentially the same.

GET and POST are generated by HTTP rules and browser/server restrictions to distinguish them in the application process.

The entire request process looks something like this:

  • The client IP address sends a request.
  • The sent request packet is transmitted over the network to the remote server IP through TCP.
  • After receiving the request packet, the server IP address parses and processes the request packet. Finally, the server returns the processing result to the client through TCP.
  • The user can then view the desired response data.

The difference between

Although the nature of GET and POST requests is the same — both are TCP connections — there are some differences in everyday use.

The differences we are talking about here are actually some things that are conventionally established, and the boundaries of some differences may not be very clear.

Effect of different

Since the two methods are produced for different division of labor, their functions must be different:

  • GET is used to obtain resources from the server
  • POST is used to submit resources to the server

In fact, this is a nonsense, the reason why the list, is to more comprehensive content.

Parameter passing mode is different

This should be the most intuitive thing we can see.

  • The GET parameter is usually pass?After the URL, multiple parameters pass&Connect, such as:www.example.com?serach=bianchengsanmei&content=123.
  • The POST parameter is usually contained inrequest bodyIn the

Actually, the difference is not absolute. GET can also take parameters through params, and POST can also take parameters after the URL, but this is generally not recommended.

Different security

GET and POST have different security because of the way the parameters are passed: GET is less secure than POST because the parameters are directly exposed to the URL, so GET cannot be used to pass sensitive information.

From the point of view of transmission, they are not secure, because HTTP is transmitted over the network in plain text, as long as the network nodes capture the packet, the data packet can be obtained completely, the only way to secure transmission is encryption, namely HTTPS.

Parameter length limits vary

GET and POST pass parameters with different lengths:

  • The amount of data to be transmitted by get is small and cannot be larger than 2KB.
  • The amount of data transmitted by POST is large. Therefore, the default value is unrestricted.

Let’s be clear here: THE HTTP protocol has no Body or URL length restrictions, and most of the restrictions on URLS are for browser and server reasons.

Because processing long urls consumes more resources, the server will limit the length of urls for the sake of performance and security (to prevent malicious attacks by constructing long urls).

Parameter data types are different

GET accepts only ASCII characters, while POST has no restrictions.

Different encoding

GET request can only be URL coded (Application/X-wwW-form-urlencoded)

POST supports multiple encoding modes (Application/X-wwW-form-urlencoded or Multipart /form-data). Use multiple encodings for binary data.

Different caching mechanisms

This needs to be explained from the following points:

  • GET requests are cached by the browser, but POST requests are not, unless manually set.
  • GET request parameters are fully preserved in the browser history, while POST parameters are not.
  • Urls generated by GET can be bookmarked, but POST cannot.
  • GET is harmless when the browser rolls back, and POST resubmits the request.

Different time consumption

The difference between GET and POST request times is mainly due to:

  • GET generates a TCP packet.
  • POST generates two TCP packets.

For a GET request, the browser sends the header and data together, and the server responds with 200 (returning data). For POST, the browser sends a Header, the server responds with 100 continue, the browser sends data, and the server responds with 200 OK.

Process of POST request:

  1. The browser requests a TCP connection (first handshake)

  2. The server accepts a TCP connection (second handshake)

  3. The browser acknowledges and sends the POST header (the third handshake, which is small, so HTTP sends the first data at this point).

  4. The server returns a 100 Continue response

  5. The browser sends data

  6. The server returns a 200 OK response

The process of a GET request:

  1. The browser requests a TCP connection (first handshake)
  2. The server accepts a TCP connection (second handshake)
  3. The browser acknowledges and sends the GET header and data (the third handshake, this packet is small, so HTTP sends the first data at this time).
  4. The server returns a 200 OK response

In a good network environment, the difference between the time of sending one packet and the time of sending two packets can be ignored. In the case of poor network environment, two-packet TCP has great advantages in verifying packet integrity.

conclusion

The similarities and differences between GET and POST have summed up so much. Due to the short time, some omissions are inevitable. Welcome to make corrections.

~

Thanks for reading!

~

Learn interesting knowledge, make interesting friends, and create interesting souls!

Hello everyone, I am the author of “programming Samadhi” Hermit King, my public number is “programming Samadhi”, welcome to pay attention, I hope you can give me more advice!

You come, with expectations, I have the ink to greet! You return, regardless of gain and loss, only with aftertaste!

Both knowledge and skill, internal force and external work, both theory and practice should grasp, both hands should be hard!

References:

[1] Differences between GET and POST

[2] What are the differences between GET and POST

[3] Difference between POST and GET