Basic HTTP methods GET, POST, PUT and DELETE

1. Method introduction

1.1 GET

The GET method is used to retrieve resources from the server. This is a safe approach as it does not alter the state of the resource in any way. The GET method is idempotent, so calling this method multiple times will always give the same result.

HTTP GET 'http://www.timi.com/users'
HTTP GET 'http://www.timi.com/users?size=20&page=5'
HTTP GET 'http://www.timi.com/users/123'
HTTP GET 'http://www.timi.com/users/123/address'

1.2 POST

The POST method is used to create a new resource in the resource collection on the server.

It should be noted that POST is not idempotent. Therefore, invoking two identical POST requests will result in duplicate information being created on the server.

HTTP POST'http://www.timi.com/users/123'
HTTP POST'http://www.timi.com/users/123/address'

1.3 PUT

PUT is used to update existing resources on the server, and updates complete resources. PUT may decide to create a new resource if the resource does not exist. The PUT method is idempotent, so calling this method multiple times will always update the same resource multiple times.

HTTP POST 'http://www.apidomain.com/users/123'
HTTP POST 'http://www.apidomain.com/users/123/accounts/456'

1.4 PATCH

PATCH is used to update an existing resource on the server, it updates a part of the resource. PUT may decide to create a new resource if the resource does not exist. Just like the PUT method, PATCH is idempotent.

HTTP PATCH 'http://www.apidomain.com/users/123'
HTTP PATCH 'http://www.apidomain.com/users/123/accounts/456'

The PUT method mostly completely replaces the entire existing resource, but the PATCH partially updates the existing resource. The PATCH method is not a substitute for the PUT method. It applies a delta (diff), rather than replacing the entire resource.

1.5 DELETE

The DELETE method is used to delete a resource from the server. It deletes the resource identified by the Request-URI. The DELETE method is idempotent.

2. Advantages

POST that can be requested by PUT, PATCH and DELETE can also be requested, so why not use POST instead of the previous three methods? What are the advantages of these three methods?

2.1 Idempotency

First things first: There are four basic methods in HTTP: GET, POST, PUT, and DELETE. Most of the time we use GET. It is used for anything safe and does not cause any side effects. GETs can be bookmarked, cached, linked to and passed through a proxy server. It's a very powerful operation, a very useful one.

But compared to POST, GET is not so powerful. POST may be the most powerful operation. It can do whatever GET is capable of. Basically, many people use one POST to conquer the world. However, POST does not place any restrictions on what can happen. That means you can't do anything with it without caching, without a button, without asking the user, etc. However, the browser might look at all the links on the page and prefetch them, or prefetch the link it thinks is most likely to be followed next.

PUT and DELETE sit between GET and POST. The difference between PUT or DELETE and POST is that PUT and DELETE are idempotent, while POST is not .

PUT and DELETE
Suppose you want to create a new page at http://www.timi.com/test.html, so you type and put this page at that URL. The server then creates the page at the URL you provided. However, for some reason your network connection is interrupted, at this time it is not sure whether the request will go through, maybe the network is slow, maybe there is a problem with the proxy server. The PUT method is perfectly fine to try again, or try again -- as many times as you want. Because putting the same document into the same URL ten times is not any different than putting it once. The same goes for DELETE. You can delete ten times, which is the same as deleting once.

POST
POST may cause different things to happen each time. JAssume you are checking out from an online store by pressing the buy button. If you send that POST request again, you may end up buying all the items in your cart again. If you send it again, then congratulations, you bought 3 of these things.

2.2 URL

2.2.1 Efficiency

Most people are using POST to update and delete operations. There is no problem with this operation, but the efficiency is not as good as using PUT and DELETE. There is another very important difference between PUT and POST. Suppose you want to create a new page, and then want it to be located at a certain URL, such as: http://www.timi.com/test.html. These operations can be protected by username and password in the case of full support for PUT.

In contrast, if you enter POST http://www.timi.com/test.html., there is nothing to receive the POST request. In general, when a client wants to select a URL, PUT is used to create a new document. When posting to an existing URL, a new document can be created using POST.

Guess you like

Origin blog.csdn.net/qq_35241329/article/details/131978221