为什么不是HTTP PUT允许做一个REST API部分更新? [英] Why isn't HTTP PUT allowed to do partial updates in a REST API?

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

问题描述

谁说RESTful API中,必须通过HTTP PATCH分别支持部分更新?

Who says RESTful APIs must support partial updates separately via HTTP PATCH?

这似乎没有任何好处。它增加了更多的工作,以落实在客户端服务器端多逻辑哪一种更新的决定请求。

It seems to have no benefits. It adds more work to implement on the server side and more logic on the client side to decide which kind of update to request.

我问创建具有一个HTTP REST API,它提供的抽象已知数据模型的框架内这个问题。要求对部分更新补丁,而不是把对全部或部分给人的感觉更像OCD规范书呆子组成的规则,但我可以被说服。

I am asking this question within the context of creating a REST API with HTTP that provides abstraction to known data models. Requiring PATCH for partial updates as opposed to PUT for full or partial feels more like OCD spec nerds making up rules, but I could be persuaded.

相关

<一个href=\"http://restcookbook.com/HTTP%20Methods/idempotency/\">http://restcookbook.com/HTTP%20Methods/idempotency/ - 这意味着你没有对可能缓存请求的服务器软件控制

http://restcookbook.com/HTTP%20Methods/idempotency/ - this implies you don't have control over the server software that may cache requests.

<一个href=\"http://stackoverflow.com/questions/2364110/whats-the-justification-behind-disallowing-partial-put\">What's背后不允许偏摆的理由 - ?没有给出明确的答案,仅供参考一下HTTP PUT为VS PATCH定义

What's the justification behind disallowing partial PUT? - no clear answer given, only reference to what HTTP defines for PUt vs PATCH.

<一个href=\"http://groups.yahoo.com/neo/groups/rest-discuss/conversations/topics/17415\">http://groups.yahoo.com/neo/groups/rest-discuss/conversations/topics/17415 - 显示了这一思想的鸿沟

http://groups.yahoo.com/neo/groups/rest-discuss/conversations/topics/17415 - shows the divide of thoughts on this.

推荐答案

谁说的?谁发明REST小伙子说:

Who says? The guy who invented REST says:

@mnot Oy公司,是的,PATCH是我初始创建HTTP / 1.1建议,因为偏摆从来都不是REST风格。 ; - )

@mnot Oy, yes, PATCH was something I created for the initial HTTP/1.1 proposal because partial PUT is never RESTful. ;-)

<一个href=\"https://twitter.com/fielding/status/275471320685367296\">https://twitter.com/fielding/status/275471320685367296

首先,REST是一种架构风格,它的原则之一是利用对协议的标的是标准化的行为,所以,如果你想通过HTTP实施一个RESTful API,你必须遵循HTTP严格的它是基于REST的。你是免费的,如果你认为它是不能满足您的需求,这样做,没有人会骂你的,但你没有做REST。你必须记录在何处以及如何从标准偏差,创建客户端和服务器实现之间的强耦合,以及使用REST的整点precisely避免并专注于您的媒体类型。

First of all, REST is an architectural style, and one of its principles is to leverage on the standardized behavior of the protocol underlying it, so if you want to implement a RESTful API over HTTP, you have to follow HTTP strictly for it to be RESTful. You're free to not do so if you think it's not adequate for your needs, nobody will curse you for that, but then you're not doing REST. You'll have to document where and how you deviate from the standard, creating a strong coupling between client and server implementations, and the whole point of using REST is precisely to avoid that and focus on your media types.

因此​​,基于RFC 7231,PUT应仅用于完全替换一个重新presentation的,在一个幂等操作中使用。 PATCH应该用于部分更新,这不需要幂等,但它是一个很好的通过要求precondition或应用差异前验证当前的状态,使他们幂等。如果你需要做非幂等的更新,部分或不使用POST。简单。每个人都使用API​​谁知道PUT和PATCH作品如何希望他们工作的方式,你不必文档或说明什么方法应该对于一个给定的资源做的。你可以自由地做PUT行为在您认为合适的任何其他方式,但你必须记录,对你的客户,你就必须找到另一个流行语为您的API,因为这不是RESTful的。

So, based on RFC 7231, PUT should be used only for complete replacement of a representation, in an idempotent operation. PATCH should be used for partial updates, that aren't required to be idempotent, but it's a good to make them idempotent by requiring a precondition or validating the current state before applying the diff. If you need to do non-idempotent updates, partial or not, use POST. Simple. Everyone using your API who knows how PUT and PATCH works expects them to work that way, and you don't have to document or explain what the methods should do for a given resource. You're free to make PUT act in any other way you see fit, but then you'll have to document that for your clients, and you'll have to find another buzzword for your API, because that's not RESTful.

记住,REST是一种架构风格专注于您的API的长期演进。做是正确的将会的补充更多的工作了,但会做出改变更容易,创伤小版本。这并不意味着REST是足够的一切,每一个人。如果你的重点是易于实施和短期使用的,因为你只想使用的方法。你可以通过做一切POST如果你不想理会客户选择合适的方法。

Keep in mind that REST is an architectural style focused on long term evolution of your API. To do it right will add more work now, but will make changes easier and less traumatic later. That doesn't mean REST is adequate for everything and everyone. If your focus is the ease of implementation and short term usage, just use the methods as you want. You can do everything through POST if you don't want to bother about clients choosing the right methods.

这篇关于为什么不是HTTP PUT允许做一个REST API部分更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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