Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PATCH vs PUT for Update Bill

Tags:

rest

put

patch

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.

like image 578
j10 Avatar asked Mar 04 '23 11:03

j10


1 Answers

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.

like image 144
Barmar Avatar answered Apr 26 '23 21:04

Barmar