PATCH vs PUT for Update Bill

核能气质少年 提交于 2019-12-10 11:25:32

问题


We have an inventory feature where we generate Bills. There is a Edit Bill API call. We are confused to implement this as PATCH Or PUT.

let's say our BillLineItem consists of

{
   stockId
   quantity
   rate
}

A Bill with id = 1 has 2 LineItems :

|  Stock Id |   Qty        |  Rate       |
|    10     |      2       |    10       |
|    11     |      3       |    20       |

Now lets say I want to change the quantity for stock Id : 10 to 5 and I want to change the rate for stock Id : 11 to 40

Should I represent this as PUT Call like :

bill : {
id : 1

lineItems : [
{
    stockId : 10,
    qty : 5,
    rate : 10   
 },

 {
    stockId : 11,
    qty : 3,
    rate : 40   
 }
]
}

Should I represent this as PATCH Call like :

 bill : {
    id : 1

    lineItems : [
    {
        stockId : 10,
        qty : 5,
     },

     {
        stockId : 11,
        rate : 40   
     }
    ]
    }

There are other parameters like discountType, discountValue as part of BillLineItem which I have not shown in the above example.


回答1:


Wikipedia describes the typical way that HTTP methods correspond to RESTful operations:

PUT - Replace all the representations of the member resources of the collection resource with the representation in the request body, or create the collection resource if it does not exist.
PATCH - Update all the representations of the member resources of the collection resource using the instructions in the request body, or may create the collection resource if it does not exist.

Since you're updating individual properties of bill items rather than replacing them completely, PATCH is the appropriate method.




回答2:


We are confused to implement this as PATCH Or PUT.

Both PUT and PATCH have remote authoring semantics; loosely, both of these are requests that the server change its copy of a resource to have the same representation as the client's copy.

PUT is a direct "make your copy like the copy I've included in this request". PATCH is "apply these changes to your copy".

It follows that PUT is idempotent; two copies of the same request have the same effect as a single copy of the request. This semantic hint can be important on an unreliable network -- if a PUT request is lost, we can just resend it. Furthermore, the hint that it is safe to resend the message is both standard and included in the message itself; that means that any generic component that sees the message will know that it is safe to resend.

PATCH does not have idempotent semantics built into it -- if a message is lost, a generic component does not know whether or not the request can safely be repeated, and therefore it needs to fail, rather than retry.

That's a pretty big win for PUT right there.

However, when a resource is very large (meaning, much larger than the HTTP headers), and the size of the change you are making is small, then it may make sense to send only the revisions to the representation, rather than the entire thing. In that case, using PATCH may be a better choice.

Part of the point of REST is that we communicate using "readily standardizable forms", so you should prefer, where possible, to support standardized patch document formats (application/json-patch+json, or application/merge-patch+json), rather than inventing your own bespoke representation.



来源:https://stackoverflow.com/questions/57816417/patch-vs-put-for-update-bill

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!