Difference between GET and POST

A GET request
is to obtain (GET) the specified resource from the server. When making a GET request, you must also tell the server the URL of the requested resource, as well as some header information, for example.


The request parameter is the content after the question mark following the url. The request parameter name and the request parameter value are connected by an equal sign (=); if there are multiple requests, they are connected by the & character. You can see the requested parameter information in the address bar of the browser.
The length of the request parameters that can be sent by the GET request is limited, and it is not suitable to use the GET method for a large amount of data. In this case, you can use POST instead.

POST request
is to publish (POST) information to the server at the time of request. For large or complex information publishing (such as file upload), POST is basically used. example


POST puts the request parameters in the last message body. Since the content length of the message body is not limited, POST is used to send a large amount of data;
and since the request parameters are moved to the message body, the request will not appear on the address bar. Parameters, for some sensitive information, such as passwords, even if the length is not long, a POST request will be used.

How to choose POST and GET
1. The length of the request parameters following the url of the GET request is limited; if the request parameters are too long, or a large amount of data such as file uploads, POST should be used instead

2. GET request parameters will appear in the address bar. For sensitive or security-concerned parameters (such as credit card number, user name, password, etc.), POST should be used instead.

3. The request parameters of the POST request will not appear on the address bar, so it cannot be added to the browser's bookmarks. If some pages are presented in different screens according to the request parameters, and you want to allow users to set bookmarks, so that In the future, you can directly click the bookmark to browse, you should use the GET request

4. Some browsers will cache data according to the URL. If the URL is the same URL, the data will be directly retrieved from the browser cache without actually sending a request to the server to query the latest data. If you don't want the server state to change, but the browser still gets old data from the cache, you can use a POST request instead (using a GET request can also avoid caching, such as appending a timestamp to the URL, so that the URL of each GET request are not the same

In addition, there is another non-functional consideration, which is actually one of the purposes of distinguishing GET and POST when HTTP was originally designed, which is to decide whether to use GET or POST according to whether the request is an idempotent operation. The so-called idempotent operation refers to whether the requested operation changes the server state, and whether the same operation is repeated many times and returns the same result.

1. GET requests should be used for idempotent operations. A GET request purely fetches a resource without changing the data or state on the server. The request parameters of GET are only used to inform the server that the content to be responded must be further identified according to the request parameters (instead of the URL). The same GET request is sent multiple times with the same request parameters, and the same result should be returned. .

2. POST requests should be used for non-idempotent operations. The data sent by the POST request may affect the data or status on the server, such as modifying (adding, deleting, modifying) the contents of the database, or saving files on the server. If the request would change the state of the server, you should use a POST request instead.

That is, GET gets data from the server; POST uploads data to the server.

Reference: "JSP&Servlet Study Notes" (Second Edition)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325259912&siteId=291194637