Understand the difference between post and put, by the way, the restful

Source: Internet
Author: User

Both of these methods can update resources at a glance, but there are essential differences between

Specific definition can Baidu, I do not post here, Light said my own understanding

The first explanation is idempotent, idempotent is a term of mathematics, for a single input or no input operation method, if each time is the same result, it is called idempotent

For two parameters, if the incoming value is equal, the result is equal to each incoming value, it is called idempotent, such as Min (A, B)

POST

Used to submit requests, can update or create resources, non-idempotent

For example, in our payment system, the function of an API is to create a collection amount of two-dimensional code, which is related to the amount, each user can have more than two-dimensional code, if the successive calls will create a new QR code, this time with the post

PUT

Used to route update resources to the specified URI, idempotent

Or that example, the user's account QR code is only associated with the user, and is one by one corresponding to the relationship, at this time the API can be put, because each time it is called, will refresh the user account two-dimensional code

For example, an interface for user-generated, received data is user name, password and other related information, then use post

RESTful suggests that all URIs are corresponding resources, so creating a user should not be understood as a behavior where this interface is named:

/user/creation

Creates a new user each time it is called (assuming that the user name can be duplicated)

The Put method is more concerned with the URI of a specific resource, such as updating the current user information, which can be put

/user/me/update

Here with me to refer to the current user, if it is for more user-appropriate interface, you can consider

/user/{uid}/update

Note that the same interface is called multiple times, so long as the submitted data is consistent, the user information is consistent every time, which results in the same result: a specific resource on the server has been updated

When you need to modify a specific resource in the form of an update, how do you decide whether to use put or post?

Quite simply, if the update corresponds to a URI that matches the result of multiple invocations, the put

such as updating a blog post, because the article has a single specific URI, so each update to submit the same content, the results are consistent

/blog/{document_id}/update

Each time the update commits the same content, the final result is inconsistent, using the post

As a very common example, the function of an interface is to reduce the current balance by one value, each commit specifying the value is 100, the interface is as follows

/amount/deduction

Call once, your balance-100, call two times, balance-200

This is the time to use post

4 different levels of restful

Representational status transfer

Personally understood as: the expression of the form of the state transfer

1, only one interface to exchange XML to achieve the entire service

Currently our mobile site service is similar structure, we have two URI interface/mapp/lead and/msdk/safepay

2, each resource corresponds to a specific URI, better than 1 maintenance, but the problem is still obvious, the resource version updates will introduce time-stamp maintenance, resource acquisition and update modification must correspond to different URIs

The current static content (including HTML files) for both PC master and mobile sites is this form

3, in the 2 based on the use of HTTP verb, each URI can have different actions, take full advantage of the HTTP protocol, so naturally incredibly full advantage of the HTTP protocol, such as cache and robustness

HTML4.0 only supports post and get, so both the delete and put operations use post to simulate

In the view of Web developers, if there is data change, use post, if not, use get

So at present, Chinese users see, PC-side implementation of restful very difficult, only mobile support HTML5 browser, can let the front-end to make a try

4, now seems to be more than practical application, Hypemedia control, that is, the intention of restful, reasonable architecture and network-based design to combine to bring a more convenient, powerful communication architecture

This is a little vague, but a hard way, think about it, later to pay water, open the browser, input I have to pay water, on automatic positioning + automatic placement + Auto Payment + Automatic display results, complete the process of payment of water, this is how convenient to understand! Gwy to be unemployed have wood, that help terrible do very simple things, productivity development 1th to eliminate is to hinder productivity development of backward relations of production ...

(turn) understand the difference between post and put, by the way, the restful

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.