The difference between put and post usage in the HTTP protocol

Source: Internet
Author: User

It is argued that you should use post to create a resource, use a put to update a resource, and some argue that you should use put to create a resource, use post to update a resource, and the idea that you can create or update a resource with either a put or post. These views only see the style, arguing about which style is better, in fact, put or post, not to see whether it is to create or update the resources of the action, this is not the problem of style, but the question of semantics.

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

"Methods can also has 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. "

The above is to say that if a method is repeated several times, the effect is the same, that is idempotent.

In programming. The characteristic of a idempotent operation is that its arbitrary execution has the same effect as one execution. Idempotent functions, or idempotent methods, are functions that can be executed repeatedly with the same parameters and can obtain the same result. These functions do not affect the state of the system, and do not worry that repeated execution will cause changes to the system. For example, the "settrue ()" function is a idempotent function, and the result is the same no matter how many times it executes. A more complex operation Idempotent Guarantee is achieved using a unique transaction number (serial number).

To give a simple example, if there is a blog system to provide a Web API, the pattern is so http://superblogging/blogs/post/{blog-name}, very simple, will {Blog-name} replaced with our blog name, To send an HTTP put or POST request to this URI, the body part of HTTP is the blog post, which is a very simple rest API example. Should we use the Put method or the Post method? Depending on whether the behavior of this rest service is idempotent, what is the server side behavior if we send two http://superblogging/blogs/post/Sample requests? If there are two blog posts, it means that the service is not idempotent, because the use of multiple uses has a side effect, if the latter request to overwrite the first request, then the service is idempotent. In the former case, the Post method should be used, and in the latter case, the put method should be used.

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

The difference between put and post usage in the HTTP protocol

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.