Home > Software engineering >  How to change a "not idempotent" property in RESTful API?
How to change a "not idempotent" property in RESTful API?

Time:09-23

My API RESTful Structure is :

{
    "users":[
        {
            "id": 100,
            "idempotent_property": 10,
            "not_idempotent_property": 10
        }
    ]
    
}

If I want to change only the value of "idempotent_property", I do : PATCH /users/100 > body : {"idempotent_property": 17}

But How must I do if I want to change only the value of "not_idempotent_property" ? PATCH must be used only for idempotent objet/property, isn't it ?

I think to this : POST /users/100/not_idempotent_property > body : {"value": 15}

CodePudding user response:

Find some documentation what this patch actually does. For example if you pass 10 it might add 10 somewhere in a database, clearly not idempotent.

The solution is that you call PATCH for this property exactly once. Or maybe you read the value, and Patch and check the new value.

The problem is really when PATCH returns a failure and you don’t know if the operation has been performed or not. For idempotent properties you can just pATCH again.

BTW By definition PATCH is Not idempotent.

CodePudding user response:

A PATCH is not necessarily idempotent, although it can be. Contrast this with PUT; which is always idempotent. The word "idempotent" means that any number of repeated, identical requests will leave the resource in the same state. For example if an auto-incrementing counter field is an integral part of the resource, then a PUT will naturally overwrite it (since it overwrites everything), but not necessarily so for PATCH.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH

Not sure what you mean by "idempotent objet/property". Afaik. operations can be idempotent, not properties. If you mean that the value of the property changes between you send the response and send a new request based on the response, then you can version the resource state and send for example the if unmodified since or if match header with the PATCH request.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Unmodified-Since

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Match

  •  Tags:  
  • rest
  • Related