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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With