When do you use POST and when do you use GET?

2018-12-31 01:14发布

From what I can gather, there are three categories:

  1. Never use GET and use POST
  2. Never use POST and use GET
  3. It doesn't matter which one you use.

Am I correct in assuming those three cases? If so, what are some examples from each case?

27条回答
人间绝色
2楼-- · 2018-12-31 01:42

My general rule of thumb is to use Get when you are making requests to the server that aren't going to alter state. Posts are reserved for requests to the server that alter state.

查看更多
孤独寂梦人
3楼-- · 2018-12-31 01:43

Use GET if you don't mind the request being repeated (That is it doesn't change state).

Use POST if the operation does change the system's state.

查看更多
不流泪的眼
4楼-- · 2018-12-31 01:46

This traverses into the concept of REST and how the web was kinda intended on being used. There is an excellent podcast on Software Engineering radio that gives an in depth talk about the use of Get and Post.

Get is used to pull data from the server, where an update action shouldn't be needed. The idea being is that you should be able to use the same GET request over and over and have the same information returned. The URL has the get information in the query string, because it was meant to be able to be easily sent to other systems and people like a address on where to find something.

Post is supposed to be used (at least by the REST architecture which the web is kinda based on) for pushing information to the server/telling the server to perform an action. Examples like: Update this data, Create this record.

查看更多
公子世无双
5楼-- · 2018-12-31 01:46

POST can move large data while GET cannot.

But generally it's not about a shortcomming of GET, rather a convention if you want your website/webapp to be behaving nicely.

Have a look at http://www.w3.org/2001/tag/doc/whenToUseGet.html

查看更多
栀子花@的思念
6楼-- · 2018-12-31 01:49

Another difference is that POST generally requires two HTTP operations, whereas GET only requires one.

Edit: I should clarify--for common programming patterns. Generally responding to a POST with a straight up HTML web page is a questionable design for a variety of reasons, one of which is the annoying "you must resubmit this form, do you wish to do so?" on pressing the back button.

查看更多
有味是清欢
7楼-- · 2018-12-31 01:50

Apart from the length constraints difference in many web browsers, there is also a semantic difference. GETs are supposed to be "safe" in that they are read-only operations that don't change the server state. POSTs will typically change state and will give warnings on resubmission. Search engines' web crawlers may make GETs but should never make POSTs.

Use GET if you want to read data without changing state, and use POST if you want to update state on the server.

查看更多
登录 后发表回答