0

我正在设计一个 HTTP API。

我有一个具有 Balance 属性的 Card 资源,客户可以添加/减去该属性。

起初我认为这应该实现为 PUT,因为它是对资源的一种更新形式,但后来我读到 PUT 是幂等的,但增加一个数量不是幂等的。

由于它不是对象的创建,我想我只能将其称为控制器,例如:

POST example.org/card/{card-Id}/AddToBalance

data: value=10

将在余额中增加 10。

有没有更好的办法?

4

2 回答 2

1

Yea, use cases like these are not where REST excels (expressing operations, particularly when they only affect a small subset of an entities data). Your particular case is pretty simple though, you can handle it with a slight change to your verb and endpoint:

PUT example.org/card/{card-Id}/balance

{"value" : 100}

Basically read as "Update the balance of card {id} to 100". On the server side you will still need to validate the transaction, and determine wether its a valid add based off the existing value of the balance.

于 2013-01-21T15:13:02.763 回答
0

就 REST 原则而言,设计看起来不错。

PUT 动作应该是幂等的。但这取决于你的要求

其他你可以使用PATCH的东西,因为你只是在做部分更新而不是完全替换资源。

于 2013-01-21T14:09:40.050 回答