preface

This may seem like a very basic question, but it actually covers a lot of ground, which is one of the reasons interviewers like it so much. In that case, let’s get to the bottom of their differences.

First, let’s popularize some knowledge points.

What is the HTTP

HTTP hypertext Transfer protocol, a simple request-response protocol, usually runs on top of TCP. It specifies what messages the client might send to the server and what responses it might get.

HTTP request methods

The serial number methods describe
01 HEAD Requests the specified page information and returns the entity body.
02 GET Similar to theGETRequest, except that there is no concrete content in the response returned to get the header.
03 POST Submit data to a specified resource for processing requests (such as submitting a form or uploading a file). The data is contained in the request body.POSTThe request may result in the creation of new resources or the modification of existing resources.
04 PUT Data transferred from the client to the server replaces the contents of the specified document.
05 DELETE Asks the server to delete the specified page.
06 CONNECT Reserved in HTTP/1.1 for proxy servers that can pipe connections.
07 OPTIONS Allows clients to view server performance.
08 TRACE The command output displays the requests received by the server for testing or diagnosis.
09 PATCH Is a complement to the PUT method and is used to perform local updates on known resources.

Now let’s analyze the GET request.

A GET request

  1. It is aHTTPCommon types of requests, most commonly to the server for some information.
  2. You can append query parameters toURLEnd to send the information to the server.
  3. There are requirements for the format of the query string, and each parameter name and value must be usedencodeURIComponentCode it to put it inURLAt the end, must be used&Symbol separation. For example: baidu.com/index.php?name=111&id=222
  4. A GET request generates a packet. For GET requests, the browser sends both HTTP headers and data, and the server responds with 200.

A POST request

  1. It is aHTTPCommon types of requests, most commonly to send information to the server that should be saved or to query for some information.
  2. Request parameters can be added to add the request parameters to the body.
  3. POSTThe request generates 2 packets, and in Firefox, generates 1 packet.

The difference between GET and POST requests

  1. GETThe request parameters of the request are added toheadIn, can be inurlCan be seen in;POSTThe request parameters of the request are added toBODY, in theurlIs not visible in.
  2. GETRequest parameters need to be usedencodeURIComponentTo encode, you must use&Symbol separation.
  3. The request ofurlThere is a length limit, which is limited by the browser andwebDetermined and set by the server. For example, Internet ExplorerURLThe maximum limit is 2083 characters. If you exceed this number, the submit button does not respond. becauseGETThe request parameter is added toURL, soGETThe request ofURLThe request parameter length also needs to be taken into account. whilePOSTRequests do not take into account the length of the request parameters.
  4. GETThe request generates a packet;POSTThe request generates 2 packets, and in Firefox, generates 1 packet. The difference lies in the browser’s request mechanism, which sends the request header first, then the body. becauseGETThere is no request body, so a packet is sent, andPOSTContains the request body, so sends two packets, but because Firefox has a different mechanism, sends one packet.
  5. Due to theGETThe requested parameters are inurl, so it can be opened directly in the browser
  6. GETRequests are actively cached by the browser, leaving a history, andPOSTNot by default.
  7. GETIs idempotent, andPOSTIt isn’t. (Idempotent means to perform the same operation with the same result.)

Usage scenarios for GET and POST requests

If you want faster queries, you can use GET requests. Everything else, no difference.

Leave a message to reassure

As some friends left messages to me, saying that there were differences in some content, I would like to make a unified explanation here.

  • Problem 1: GET requests can support BODY passing request parameters. The blogger didn’t mention it.

Answer: 1. By default, BODY is not supported to pass the request parameters. 2. You can use other methods to support BODY passing request parameters, but this is not officially recommended. 3. After all, this is not a specification and there are unknown compatibility issues. So I didn’t emphasize this, because there are compatibility issues and it takes a lot of effort to develop, but if you’re just passing the request parameters for the BODY, why not just POST it?

  • Question 2: GET request, POST request also involves whether bookmarks can be bookmarked.

Answer: 1. Both GET and POST requests can be bookmarked. If a request parameter exists, the POST request cannot be bookmarked. Because the request parameters for POST are in the BODY. 2. In actual project development, most interfaces need to authenticate identity information. At this point, bookmarking doesn’t make much sense. Because the information is not available. 3. The blogger thinks this is not a big distinction, for the reasons above, so it is not covered.