本文介绍了PATCH 与 PUT 更新法案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个可以生成账单的库存功能.有一个 Edit Bill API 调用.我们很困惑将其实现为 PATCH 或 PUT.

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.

假设我们的 BillLineItem 包括

let's say our BillLineItem consists of

{
   stockId
   quantity
   rate
}

id = 1 的账单有 2 个 LineItems :

A Bill with id = 1 has 2 LineItems :

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

现在假设我想更改股票 ID 的 数量:10 到 5 和我想更改股票 ID 的 rate:11 到 40

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

我是否应该将其表示为 PUT 调用如下:

Should I represent this as PUT Call like :

bill : {
id : 1

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

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

我是否应该将其表示为 PATCH 调用如下:

Should I represent this as PATCH Call like :

 bill : {
    id : 1

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

     {
        stockId : 11,
        rate : 40
     }
    ]
    }

在 BillLineItem 中还有其他参数,例如 discountType、discountValue,我没有在上面的示例中显示.

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

推荐答案

维基百科描述了 HTTP 方法对应于 RESTful 操作的典型方式:

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

PUT - 将集合资源的成员资源的所有表示替换为请求正文中的表示,或者如果集合资源不存在则创建集合资源.
PATCH - 更新集合资源的成员资源的所有表示使用请求正文中的指令,或者可以创建集合资源,如果它不存在.

由于您要更新账单项目的个别属性而不是完全替换它们,PATCH 是合适的方法.

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

这篇关于PATCH 与 PUT 更新法案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 15:33
查看更多