7

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?

1 Answer 1

8

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.

4
  • The one thing which I do not understand is idem-potency is achieved by the Http server? I can try to create a Strudent with PUT and POST. So while creating the Student my service method can create the Student as many times as the PUT or POST request hit.
    – Sam
    Aug 28, 2013 at 11:26
  • 1
    @Sam you would make sure that your code on the server is enforcing the idempotence property for PUTs. I don't understand your last sentence. Here's more on idempotence: stackoverflow.com/questions/7016785/…
    – Eric Stein
    Aug 28, 2013 at 11:49
  • 1
    You mean I have to make sure that I idem-potency is ensured. Then that way PUT or POST would be just the terminologies. I will try to create a Student via PUT 3 times and same I do with POST. So with PUT wont it create Student 3 times? This is what I meant by my last sentence.
    – Sam
    Aug 28, 2013 at 11:58
  • 1
    POST is typically used to create resources where the end user doesn't know the unique ID for the resource. So three POSTs would create three new resources, each with a server-assigned ID. If you create a resource with a PUT, you must specify all the values, including the unique ID.
    – Eric Stein
    Aug 28, 2013 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.