locked
How to show web api put verb is idempotent RRS feed

  • Question

  • User-1223304583 posted

    i searched google a lot still got no good explanation with example. mostly all write up is saying put is idempotent and post is not but i need a sample code which discuss or show idempotency of put method. so my request anyone can explain the put idempotency with example code or redirect me to right article link which discuss the same with example code. thanks

    Tuesday, February 21, 2017 8:12 AM

Answers

  • User753101303 posted

    Hi,

    It comes from http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

    You don't find any sample code because it is just a specification. Once again technically speaking you can do absolutely whatever you want in response to a put request. But following the specification PUT is supposed to be used this way :

    "The PUT method requests that the enclosed entity be stored under the   supplied Request-URI. If the Request-URI refers to an already   existing resource, the enclosed entity SHOULD be considered as a   modified version of the one residing on the origin server. If the   Request-URI does not point to an existing resource, and that URI is   capable of being defined as a new resource by the requesting user   agent, the origin server can create the resource with that URI."

    So in short it is to update (or create) something at a supplied uri. So if you do that several time you'll just update (or create then update) the same way the same resource and issuing the request one time or multiple times won't make a difference (ie it is "idempotent").

    Then the idea is just to reuse this existing "standard" rather than inventing again some other new convention. So PUT should be used to update (or even create) something at a know and fixed location.

    There is really nothing more to understand. It is not a technical limitation. It is just that you have a specification that tells what PUT is supposed to do and it is likely best to follow this specification rather than reinventing your own for your web APIs.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 21, 2017 9:16 AM