- POST = new
- GET = Read
- PUT = Update
- Delete = Erase
PUT will display parameter information in the address bar, not safe!
Understand the difference between post and put, by the way restfu
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 ...
Later on in the design API only Web service, it is often unclear whether to use put or POST, only to find that I was the Rails of the eagle frame of the case (framed by the idea of it), the so-called put can actually be used to add, and there is a somewhat new HTTP Ver B is called PATCH, and it starts with the Github API and Rails 4.
The correct definition of PUT is Replace (Create or Update), for example, PUT /items/1 replacing, /items/1 if it already exists, is not added. PUT must contain items/1 all of the material.
But this behavior does not seem to be easy to use, if only for the update of one of the components items/1 , it is necessary to re-upload all items/1 the sex is also too much bandwidth, so there are new PATCH Method standard, can be used to do partial updates (partial update).
Use a few Ruby code for example:
POST NEW:
# POST /itemsdef create @item = Item.new @item.attributes = { :name => params[:name], :image => params[:image] } @item.saveend
PUT replacement (new or full update), in this case, if the image parameters are not spread, it will be updated to null:
# PUT /items/{id}def replace @item = Item.find_by_id(params[:id]) unless @item # if @item.nil? @item = Item.new @item.id = params[:id] end @item.attributes = { :name => params[:name], :image => params[:image] } @item.saveend
Patch section Update, in this example, if the image parameters are not spread, it will not be updated:
# PATCH /items/{id}def patch @item = Item.find(params[:id]) @item.attributes = params.slice(:name, :image) @item.saveend
Delete, in this case, no matter how ITEMS/1 is not present at all.
# DELETE /items/{id}def destroy @item = Item.find_by_id(params[:id]) @item.destroy if @itemend
Sometimes it's hard to think about the "language" thing. which HTTP method to use when designing the REST API, because sometimes it's not necessarily a crud form. I think the "idempotent" feature in 9.1 Safe and idempotent Methods definition somewhat practical. Idempotent means that if the same operation is executed again for the second time, the result is the result of the first pass (that is, no matter how several times it is done, the result is only executed once). According to the specifications of HTTP, GET, HEAD, PUT and DELETE are idempotent, the same Request is executed again, the result is the same. Only POST and patch are not idempotent,post again, a new piece of information is added, and the patch is a possibility of not being able to prove idempotent (for example). Both post and patch are not idempotent operations, and this is why the Github API uses post as a compatible replacement for patches.
Another HTTP Methods feature is "safe", which is simpler and only GET and HEAD are safe operations. The Safe feature will affect whether the cache can be fast (Post/put/patch/delete must not be fast). The idempotent feature will affect whether Retry (re-try, anyway).
|
SAFE? |
idempotent? |
| GET |
Y |
Y |
| POST |
N |
N |
| PATCH |
N |
N |
| PUT |
N |
Y |
| DELETE |
N |
Y |
Through the idempotent feature, sometimes you can help you decide which HTTP Methods to use. Back to the previous PUT seems to be not very useful, for example, the browser for the main HTML-based table, to hemp is the POST new information, to hemp is to use patches to update the existing information (you do not want to use put to modify personal information, each time to re-upload photos), so less use put. This is also why rails 4 changed the put of the list to patch Method, and through the Rails Eagle Shelf, the update is actually in line with patch lines rather than put.
But there are situations where I think somewhat is suitable for put, such as whether to subscribe to post or put?
POST /subscriptions# 還是PUT /subscriptions/{something_id}
There are only two status, "subscribed" or "not subscribed", this subscribe operation is re-sent several times, or "subscribed", so I think somewhat fits the idempotent feature of PUT. And the unsubscribe API of the response is like, of course,
DELETE /subscriptions/{something_id}
Another put example that I think is interesting and practical is the design API for mobile devices that can be used by the offline (such as the iphone). Support for offline, usually using the UUID to produce the ID, this does not need to pass through the central server control ID, convenient installation of synchronization. In this scenario, the new REST API can actually provide two kinds of information:
POST /items # 參數帶 uuid=4937973d-e349-460a-a6ad-38625125b24a。如果不帶uuid則由server來產生uuid# 和PUT /items/4937973d-e349-460a-a6ad-38625125b24a
For the mobile client, the problem with post is that it is not stable in the off-line environment, there is a possibility that there is no return after post, so there is no synchronization success, so it is time to try again (retry), but it explodes with post, because the server will create a new pen The UUID duplicate information. But with put there is no problem, put is idempotent operation, can be re-sent no relationship (can be paired with Conditional PUT machine, with the ETag and last-modified Headers to ensure that the conflict is not covered)
If the client does not have offline requirements, such as third-party applications, then the Post/items API can be used to give the server a UUID.
HTTP POST, PUT PATCH