REST - 修改资源的一部分 - PUT或POST [英] REST - Modify Part of Resource - PUT or POST

查看:214
本文介绍了REST - 修改资源的一部分 - PUT或POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在看到如何使用REST更新资源的一部分(例如状态指示器)的主题。

I'm seeing a good bit of hand-waving on the subject of how to update only part of a resource (eg. status indicator) using REST.

选项似乎是:


  1. 抱怨HTTP没有PATCH或MODIFY命令。但是, REST的HTTP MODIFY动词?上接受的答案可以很好地展示为什么这不像它看起来那么好。

  1. Complain that HTTP doesn't have a PATCH or MODIFY command. However, the accepted answer on HTTP MODIFY verb for REST? does a good job of showing why that's not as good an idea as it might seem.

使用POST参数并识别方法(例如名为action的参数)。一些建议是使用自定义方法名称指定X-HTTP-Method-Override标头。这似乎导致了在实现过程中根据你想要做的事情进行切换的丑陋,并且批评不是一种特别的REST方式来使用POST。事实上,采用这种方法开始感觉像是一个RPC类型的接口。

Use POST with parameters and identify a method (eg. a parameter named "action"). Some suggestions are to specify an X-HTTP-Method-Override header with a self-defined method name. That seems to lead to the ugliness of switching within the implementation based on what you're trying to do, and to be open to the criticism of not being a particularly RESTful way to use POST. In fact, taking this approach starts to feel like an RPC-type interface.

使用PUT覆盖代表资源的资源的子资源要更新的特定属性。事实上,这实际上是对子资源的重写,这似乎符合PUT的精神。

Use PUT to over-write a sub-resource of the resource which represents the specific attribute(s) to update. In fact, this is effectively an over-write of the sub-resource, which seems in line with the spirit of PUT.

此时,我认为#3是最合理的选择。

At this point, I see #3 as the most reasonable option.

这是最佳做法还是反模式?还有其他选择吗?

Is this a best practice or an anti-pattern? Are there other options?

推荐答案

选项3(PUT到一些独立的子资源)是你现在最好的选择,它在主要资源本身上使用POST不一定是错误的 - 尽管你可能不同意这取决于你想要的迂腐程度。

Option 3 (PUT to some separated sub-resource) is your best bet right now, and it wouldn't necessarily be "wrong" to just use POST on the main resource itself - although you could disagree with that depending on how pedantic you want to be about it.

坚持使用3并使用更细粒度的子资源,如果你确实需要类似PATCH的行为 - 请使用POST。就个人而言,即使PATCH确实最终成为一个可行的选择,我仍然会使用这种方法。

Stick with 3 and use more granular sub-resources, and if you really do have a need for PATCH-like behavior - use POST. Personally, I will still use this approach even if PATCH does actually end up as a viable option.

这篇关于REST - 修改资源的一部分 - PUT或POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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