HTTP request method and idempotency

Programmer light 2022-02-13 07:52:18 阅读数:554

http request method idempotency

This paper briefly

Hello, students ! This article mainly introduces HTTP Idempotency .HTTP The idempotence of a method means that a resource should have the same side effects when it is requested once or multiple times .

  • Blogger : Programmer light , An old rookie , Ha ha ha ha

  • One can walk very fast , A group of people can walk far

  • give the thumbs-up Comment on Collection   Focus on == Develop habits ( Four in one
  • Welcome to study and discuss together ️ Progress together
  • The author's level is limited , You are welcome to point out , Learn from each other and make progress !

 

One 、HTTP Request method

Two 、 Idempotency

2.1、HTTP GET Method

2.2、HTTP POST Method

2.3、HTTP PUT Method

2.4、HTTP PATCH Method

2.5、HTTP DELETE Method

3、 ... and 、 How to design high-quality products that conform to idempotence RESTful API

3.1、GET  vs POST

3.2、POST vs PUT

3.3、PUT vs PATCH


One 、HTTP Request method

according to HTTP standard ,HTTP Requests can be made in a variety of ways .
GET POST HEAD OPTIONS PUT PATCH DELETE TRACE CONNECT.

index method description
1 GET Request specific page information , And return the entity body
2 HEAD Be similar to GET request , But there is no specific content in the response returned , For getting headers
3 POST Submit data to the specified resource for processing request ( For example, submit a form or upload a file ). Data is contained in the request body .POST Requests may lead to the creation of new resources and / Or modification of existing resources .
4 PUT The data transmitted from the client to the server replaces the content of the specified document .( Replace all )
5 PATCH The data transmitted from the client to the server replaces the content of the specified document .( To partially replace )
6 DELETE Request the server to delete the specified page .
7 CONNECT HTTP/1.1 The protocol is reserved for the proxy server that can change the connection to pipeline mode .
8 OPTIONS Allow clients to view the performance of the server .
9 TRACE Echo requests received by server , Mainly used for testing or diagnosis .

That's it HTTP A general introduction to the request method , Let's understand what is HTTP Idempotency of request method .

Two 、 Idempotency

HTTP Idempotent methods , It means that no matter calling this url How many times , There will be no different results HTTP Method . That is, no matter what you call 1 It's still a call 100 Time ,1000 Time , The results are the same ( The premise is that the data on the server side has not been changed manually . for instance , The data in your database has been manually changed , The results of those two calls must have changed ).

Let's discuss GET POST PUT PATCH DELETE Idempotency .

GET /tickets # obtain ticket list
GET /tickets/12 # Look at a specific ticket
POST /tickets # Create a new one ticket
PUT /tickets/12 # to update ticket 12
PATCH /tickets/12 # to update ticket 12
DELETE /tickets/12 # Delete ticekt 12

2.1、HTTP GET Method

HTTP GET Method , Used to get resources , No matter how many interfaces are called , The results will not change , So it's idempotent .

GET /tickets # obtain ticket list
GET /tickets/12 # Look at a specific ticket

Just query the data , It won't affect the change of resources , So we think it's idempotent .
It is worth noting that , Idempotency refers to the action on the result rather than the resource itself . How do you understand that ? for example , This HTTP GET Methods may get different returns each time , But it doesn't affect resources .
You may ask if this is the case ? Of course . for example , We have an interface to get the current time , We should design it as

GET /service_time # Get the current time of the server

It will not affect the resource itself , Therefore, idempotency is satisfied .

2.2、HTTP POST Method

HTTP POST Method is a non idempotent method , Because it's called many times , Will generate new resources

POST /tickets # Create a new one ticket

Because it has an impact on the resources themselves , Every time you call, a new resource is generated , So it doesn't satisfy idempotency .

2.3、HTTP PUT Method

HTTP PUT Is the method idempotent ? Let's see

PUT /tickets/12 # to update ticket 12

Because it directly replaces the data of the entity part with the resources of the server , We call it many times , It's only going to have an impact once , But with the same result HTTP Method , So it satisfies idempotency .

2.4、HTTP PATCH Method

HTTP PATCH Method is non idempotent .HTTP POST Methods and HTTP PUT The method may be easier to understand , however HTTP PATCH The method is only to update some resources , How is it non idempotent ?
because ,PATCH The entities provided need to be defined according to procedures or other agreements , After parsing, execute on the server , To modify the resources on the server . let me put it another way ,PATCH The request will execute a program , If you submit repeatedly , The program may be executed multiple times , There may be additional impact on the resources on the server , This explains why it is non idempotent .
Maybe you don't understand this yet . Let's take an example

PATCH /tickets/12 # to update ticket 12

here , Our server's processing of methods is , When a method is called , Update some fields , Take this ticket Add one to the recorded operation record , This time, , Does the resource of each call change , So it could be a non idempotent operation .

2.5、HTTP DELETE Method

HTTP DELETE Method to delete a resource , The resource will be deleted .

DELETE /tickets/12 # Delete ticekt 12

One call and multiple calls have the same impact on the resource , So it also satisfies idempotency .

3、 ... and 、 How to design high-quality products that conform to idempotence RESTful API

3.1、GET  vs POST

Maybe , You will think of an interview question .

HTTP Requested GET And POST What's the difference ? You may answer :GET Way through URL Submit data , The data is in URL Can be seen in ;POST The way , The data is placed in HTML HEADER The submission . however , We are from now on RESTful From the perspective of resources ,HTTP GET Method is idempotent , So it is suitable for query operation ,HTTP POST Method is non idempotent , Therefore, it is used to indicate the new operation .

however , There are exceptions. , Sometimes we may need to transform the query method into HTTP POST Method . such as , Super long (1k) Of GET URL Use POST To replace , because GET suffer URL Length limitation . although , It does not conform to idempotency , But it's a compromise .

3.2、POST vs PUT

about HTTP POST Methods and TTP PUT Method , Our general understanding is POST Represents creating a resource ,PUT Represents an update resource . Of course , This is the correct understanding .


however , actually , Both methods are used to create resources , The more essential difference is in idempotency .HTTP POST The method is non idempotent , So it is used to represent the creation of resources ,HTTP PUT Method is idempotent , Therefore, it means that updating resources is more appropriate .

3.3、PUT vs PATCH

here , You see, there will be another problem .HTTP PUT Methods and HTTP PATCH Method , Are used to describe update resources , What's the difference between them ? Our general understanding is PUT Means to update all resources ,PATCH Indicates updating some resources . First , This is the first rule we follow . According to the description above ,PATCH Method is non idempotent , Therefore, we are designing our server side RESTful API When , Also need to consider . If , We want to make it clear to the caller that our resources are idempotent , My design tends to use HTTP PUT Method .

copyright:author[Programmer light],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130752144965.html