HTTP Methods Every Web Developer Must Know

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值