Difference between put and post in HTTP protocol

Source: Internet
Author: User

Some hold that post should be used to create a resource, and put should be used to update a resource; some hold that put should be used to create a resource and post should be used to update a resource; another point is that you can use either put or post to create or update a resource. These ideas only show the style. In the debate, they only argue about which style is better. In fact, using put or post doesn't mean that it is the action of creating or updating resources. This is not a style issue, it is a semantic issue.

 

In HTTP, put is defined as the idempotent method, and post is not. This is an important difference.

 

"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."

 

In the above words, if a method is repeatedly executed multiple times, the results will be the same, that is, idempotent.

 

For example, a blog system provides a web API in the http: // superblogging/blogs/post/{blog-name} mode, replace {blog-name} with our blog name and send an http put or POST request to this URI. The HTTP body is a blog, which is a simple example of rest API. Should we use the put method or the POST method? It depends on whether the behavior of this rest service is idempotent. If we send two http: // superblogging/blogs/post/sample requests, what kind of behavior is the server side? If two blog posts are generated, it indicates that this service is not idempotent, because multiple use operations have a side effect. If the next request overwrites the first one, the service is idempotent. In the previous case, the POST method should be used. In the latter case, the put method should be used.

 

Maybe you will think that the difference between the two methods is no big deal, and there will be no problems if you use the wrong method, but if your service is put on the Internet, if you do not follow the HTTP protocol specifications, it may cause troubles to you. For example, Google crawler may also access your service. If a service that is not indempotent can be accessed using the indempotent method, the status of your server may be modified by the crawler, 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.