不允许部分PUT背后的理由是什么? [英] What's the justification behind disallowing partial PUT?

查看:182
本文介绍了不允许部分PUT背后的理由是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么HTTP PUT请求必须包含整体状态的表示而不能仅仅是部分?

Why does an HTTP PUT request have to contain a representation of a 'whole' state and can't just be a partial?

我知道这是PUT的现有定义 - 这个问题是关于为什么这样定义的原因。

I understand that this is the existing definition of PUT - this question is about the reason(s) why it would be defined that way.

ie:

通过阻止部分PUT获得了什么?

What is gained by preventing partial PUTs?

为什么阻止幂等部分更新被视为可接受的损失?

Why was preventing idempotent partial updates considered an acceptable loss?

推荐答案

PUT表示HTTP规范定义的含义。客户端和服务器无法改变这种含义。如果客户端或服务器以与其定义相矛盾的方式使用PUT,则至少可能发生以下情况:

PUT means what the HTTP spec defines it to mean. Clients and servers cannot change that meaning. If clients or servers use PUT in a way that contradicts its definition, at least the following thing might happen:

根据定义,Put是幂等的。这意味着客户端(或中间人!)可以重复任何次数的PUT,并确保效果相同。假设中间设备从客户端接收PUT请求。当它将请求转发到服务器时,存在网络问题。中间人根据定义知道它可以重试PUT直到它成功。如果服务器以非幂等方式使用PUT,则这些潜在的多个调用将产生不良影响。

Put is by definition idempotent. That means a client (or intermediary!) can repeat a PUT any number of times and be sure that the effect will be the same. Suppose an intermediary receives a PUT request from a client. When it forwards the request to the server, there is a network problem. The intermediary knows by definition that it can retry the PUT until it succeeds. If the server uses PUT in a non idempotent way these potential multiple calls will have an undesired effect.

如果要进行部分更新,请使用PATCH或使用POST一个子资源并返回303参见其他'主'资源,例如

If you want to do a partial update, use PATCH or use POST on a sub-resource and return 303 See Other to the 'main' resource, e.g.


POST /account/445/owner/address
Content-Type: application/x-www-form-urlencoded

street=MyWay&zip=22222&city=Manchaster


303 See Other
Location: /account/445

编辑:关于一般问题为什么部分更新不能是幂等的:

On the general question why partial updates cannot be idempotent:

部分更新通常不能是幂等的,因为幂等性取决于媒体类型语义。 IOW,您可能能够指定允许幂等补丁的格式,但不能保证PATCH在每种情况下都是幂等的。由于方法的语义不能是媒体类型的函数(出于正交原因),PATCH需要被定义为非幂等的。并且PUT(被定义为幂等)不能用于部分更新。

A partial update cannot be idempotent in general because the idempotency depends on the media type semantics. IOW, you might be able to specify a format that allows for idempotent patches, but PATCH cannot be guaranteed to be idempotent for every case. Since the semantics of a method cannot be a function of the media type (for orthogonality reasons) PATCH needs to be defined as non-idempotent. And PUT (being defined as idempotent) cannot be used for partial updates.

这篇关于不允许部分PUT背后的理由是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆