The difference between put and post usage in the HTTP protocol

Source: Internet
Author: User

Http://hi.baidu.com/%CE%A2%D0%A6%C0%E1%CB%AE/blog/item/84fd8e3d93944350241f14cc.html

There is a view that post should be used to create a resource to update a resource with put; there is a view that you should use put to create a resource, post to update a resource, and that you can create or update a resource with any of the put and post. These views only see the style, the debate is just arguing about which style is better, in fact, put or post, not to see whether this is to create or update the resources of the action, this is not a question of style, but a semantic problem.

In HTTP, put is defined as a idempotent method, post is not, this is a very important difference.

"Methods can also have" idempotence "in", aside from error or expiration issues) the side-effects of N > 0 Identical requests is the same as a single request.

The above is to say, if a method repeats repeatedly, the effect is the same, that is idempotent.

To give a simple example, if there is a blog system that provides a web API, the pattern is so http://superblogging/blogs/post/{blog-name}, very simply, replace {blog-name} with our blog name, To send an HTTP put or POST request to this URI, the body part of HTTP is Bowen, which is an example of a simple rest API. Should we use the Put method or Post method? Depending on whether the behavior of the rest service is idempotent, if we send two http://superblogging/blogs/post/Sample requests, what is the behavior of the server side? If two blog posts are generated, it means that the service is not idempotent, because the side effects of multiple use have been, if the latter request to cover the first request, the service is idempotent. In the former case, you should use the Post method, and in the latter case you should use the Put method.

You may think that the difference between the two methods is not a big deal, with the wrong will not have any problems, but your service on the Internet, if you do not comply with the specifications of the HTTP protocol, you can cause trouble to themselves. For example, Google Crawler will also visit your service, if you let a service that is not indempotent can be accessed using Indempotent method, then your server status may be crawler modified, this should not happen.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.