Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is meant by PUT method idempotent in RESTful web service?

Tags:

rest

post

put

I am trying to decide which Http method should be used PUT or POST.

While looking at some posts on StackOverlflow I could see this post.

One of the answers in post says

PUT is idempotent, so if you PUT an object twice, it has no effect. This is a nice property, so I would use PUT when possible.

Can some one help me out here with an example. Lets say that I have a scenario where I am trying to create a student whose entry would be passed in Student table in a RDBMS.

So here if I try to PUT that entry again and again will there be no effect?

like image 649
Sam Avatar asked Aug 28 '13 10:08

Sam


People also ask

Why put method is idempotent?

A PUT request is idempotent because it only updates a record. So identical requests will ultimately result in no state change except for the first call.

What is idempotent in restful web services?

An HTTP method is idempotent if an identical request can be made once or several times in a row with the same effect while leaving the server in the same state. In other words, an idempotent method should not have any side effects — unless those side effects are also idempotent.

Why put is idempotent and POST is not?

PUT method is idempotent. So if you send retry a request multiple times, that should be equivalent to single request modification. POST is NOT idempotent. So if you retry the request N times, you will end up having N resources with N different URIs created on server.

Is get idempotent in REST API?

For example, using GET, an API can retrieve a REST resource. This has no effect on the resource and can be repeated over and over – it is both idempotent and safe.


1 Answers

In a PUT, you're setting all the values of the resource, so when the PUT is done, you know exactly what the state is of the resource. If you wait a week and call your PUT again, you still know exactly what the state is of the resource.

A POST, by contrast, is not idempotent - you only POST a subset of the values. So if you call POST today, wait a week, and make the same POST call again, you don't know what the state of the resource is - somebody might have changed a value you're not setting in the POST.

Idempotent means that no matter when or how often you make the call, the end state of the resource is exactly the same.

DELETE and GET are also idempotent.

like image 105
Eric Stein Avatar answered Nov 14 '22 21:11

Eric Stein