The use of idempotent to differentiate the post and put requests of HTTP

Source: Internet
Author: User
1. What is idempotent

Idempotent concept: idempotent means that the same results are achieved regardless of the number of repeated operations. What are idempotent operations in 2.REST requests

Get,put,delete are idempotent operations, while post is not, the following is analyzed:

First the GET request is well understood and the resource is queried multiple times, and the result of this implementation is the same.
The idempotent of a put request can be understood as a modification of A to B, the first request value changed to B, then multiple times, the final result is B, and the result of one execution is the same, so the put is a idempotent operation.
Similarly, you can understand the delete operation, after the first time the resource is deleted, the deletion request is made several times later, the end result is the same, the resource is deleted.

Post is not idempotent, because a single request adds a new resource, two requests add two new resources, and multiple requests produce different results, so post is not a idempotent operation. 3 The use of post and put in terms of idempotent distinction

Understanding rest for a long time does not clearly differentiate between put and post, and is easily confused when used, and can be distinguished entirely by idempotent (idempotent).

To give a simple example, if there is a blog system to provide a Web API, the pattern is so http://superblogging/blogs/{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.

We should use the Put method or the Post method.

Depending on whether the behavior of this rest service is idempotent, if we send two http://superblogging/blogs/post/Sample requests, what behavior is on the server side. 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.

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.