The Four (Plus One) Elements
Two of them are probably the
most frequently used by you and two (plus one) of them are rarely use
even though most of programmers are aware of their existence.
GET
“I get something!”
“Get the data and back off!”
As you might know, GET should always only getting the data from the specified resources and should not
create any action other than retrieval. If you want to save the data to
database, modify files, etc., do not use GET. This is important as GET
(and HEAD, see below) is considered “safe”.
HEAD
We can say that this is the
light version of GET because HEAD is actually the same as GET but
ignoring the response body. When do we use HEAD instead of GET? Testing
links, check for modifications, etc., that is for something that you
didn’t need to read the response body and just okay with the response
header.
POST
If GET retrieved the data then POST is a method to store data, create new resources, etc. POST to GET is like “write” to “read”. Pretty simple. (You might be wondering why I simplify things and not telling the truth about the vary responses, whether it’s 200, 201, or 204. If you’re really curious about those response codes, please refer to RFC2616: HTTP Status Code Definitions. ).
PUT
PUT, simply put, is practically the same with POST. The difference between PUT and POST is in its behavior. Read this explanation for a brief and easy-to-understand answer.
DELETE
As the name suggests, DELETE is the “opposite” of PUT. Again, DELETE to PUT is like “erase” to “write”. No big deal. (I know some people who use POST to delete or destroy the data/resource but that should not happened).
“So if PUT and DELETE are identical with POST, why we would do something using PUT and DELETE instead of POST?“, you asked.
PUT and DELETE is idempotency.
I repeat, idempotency. No matter how much you use PUT or DELETE, if you
do that more than one within the same resource, it will be counted as
one. This rule is not applicable for POST as everytime you do the POST,
the (same) data will be stored/created over and over again. Elliotte Rusty Harold explained about this in an interview six years ago.
There’s more than this. You can see another three methods: TRACE, CONNECT, and OPTIONS and the relatively new method: PATCH.
原文:http://css.dzone.com/articles/http-methods-every-web