HTTP中PUT和POST的区别

来源:互联网 发布:国家电网网络大学登录 编辑:程序博客网 时间:2024/05/26 02:51

大多数观点认为,在HTTP中应该用PUT来更新资源,POST来创建一个新的资源。也有部分观点认为,应该用POST来更新,PUT用来创建。

其实,用PUT还是POST,不是看对资源的操作,而是由于HTTP协议定义的语义问题。

在HTTP协议中,PUT被定义为idempotent(幂等)的操作,而POST是非幂等的。

Methods can also have the property of “idempotence” in that (aside
from error or expiration issues) the side-effects of N > 0 identical
requests is the same as for a single request.

意思是说:如果一个方法重复执行多次,产生的效果是一样的,那就是idempotent的。