简体   繁体   English

REST API 强制更新资源

[英]Rest api force update resource

What is a good practice for a rest api to allow a force update.什么是休息 api 允许强制更新的好习惯。 The normal update will return a warning if something is exceeded.如果超出某些范围,正常更新将返回警告。

I was thinking:我刚在想:

  1. PUT /myresource/{id}?options=FORCE PUT /myresource/{id}?options=FORCE
  2. PUT /myresource/{id}, include optional options field in payload. PUT /myresource/{id},在有效负载中包含可选的选项字段。

Any better approach?有什么更好的方法吗?

Thanks谢谢

PUT /myresource/{id}?options=FORCE

This is a bad idea that you can probably make work if you insist upon it.这是一个坏主意,如果您坚持,您可能可以完成工作。

The core idea of REST is that we have a uniform interface - all resources understand messages the same way. REST 的核心思想是我们有一个统一的接口——所有资源都以相同的方式理解消息。 In the case of PUT, that means that we all understand the message as described by RFC 7231在 PUT 的情况下,这意味着我们都理解RFC 7231所描述的消息

The target-uri of the request indicates which resource a given message applies to.请求的目标 uri 指示给定消息适用于哪个资源。 So from the perspective of general purpose components, PUT /myresource/{id}?options=FORCE means "please update the representation of /myresource/{id}?options=FORCE".所以从通用组件的角度来看, PUT /myresource/{id}?options=FORCE意思是“请更新 /myresource/{id}?options=FORCE 的表示”。

Note the deliberate inclusion of the query part, which is part of the identification of the primary resource.请注意故意包含查询部分,这是主要资源标识的一部分。

"/myresource/{id}?options=FORCE" is a different resource identifier than "/myresource/{id}", even though the hierarchical parts of the URI are the same. “/myresource/{id}?options=FORCE”是与“/myresource/{id}”不同的资源标识符,即使 URI 的分层部分是相同的。

So from the point of view of a general purpose component (like a browser, or a caching web proxy), your proposed request leaves the cached representations of "/myresource/{id}" unchanged.因此,从通用组件(如浏览器或缓存 Web 代理)的角度来看,您提出的请求使“/myresource/{id}”的缓存表示保持不变。

You can probably make it work: if you read the cache invalidation specification carefully, you will see that the target-uri is not the only URI that is invalidated by a successful response to an unsafe request;你或许可以让它发挥作用:如果你仔细阅读缓存失效规范,你会发现 target-uri 并不是唯一一个因成功响应不安全请求而失效的 URI; the cache is also expected to invalidate the resources identified by the Location and Content-Location headers.缓存还应该使由 Location 和 Content-Location 标头标识的资源无效。

So a response like:所以响应如下:

200 OK
Content-Location: /myresource/{id}

<<updated representation of /myresource/{id}

will invalidate both /myresource/{id}?options=FORCE and /myresource/{id}.将使 /myresource/{id}?options=FORCE 和 /myresource/{id} 无效。

Of course, using the Content-Location header in this way introduces other constraints.当然,以这种方式使用 Content-Location 标头会引入其他约束。

PUT /myresource/{id}, include optional options field in payload. PUT /myresource/{id},在有效负载中包含可选的选项字段。

Better - we're identifying the resource really want to modify (so the caches now understand what is going on).更好 - 我们正在识别真正想要修改的资源(因此缓存现在了解正在发生的事情)。 Since you probably don't intend that the optional fields you are using to force the update become part of the server's representation, you need to do a bit of extra fuss in the response to avoid implying that the requested representation was accepted as is.由于您可能不希望用于强制更新的可选字段成为服务器表示的一部分,因此您需要在响应中做一些额外的工作,以避免暗示所请求的表示已按原样接受。


Another option would be to consider using the Authentication header;另一种选择是考虑使用 Authentication 标头; analogously to how one would use sudo rm -rf to override the default policy.类似于如何使用sudo rm -rf覆盖默认策略。 In effect, your implementation logic is expected to check whether the author of the request has been assigned to a role that allows edits beyond those allowed by the default policy.实际上,您的实现逻辑应该检查请求的作者是否已分配给允许编辑超出默认策略允许的编辑的角色。


If you aren't satisfied that your needs align well with the existing semantics of the Authentication header, you can instead introduce a new header如果您不满意您的需求与 Authentication 标头的现有语义一致,您可以改为引入一个新标头

New header fields can be defined such that, when they are understood by a recipient, they might override or enhance the interpretation of previously defined header fields, define preconditions on request evaluation, or refine the meaning of responses.可以定义新的标头字段,以便当接收者理解它们时,它们可能会覆盖或增强对先前定义的标头字段的解释,定义请求评估的前提条件,或改进响应的含义。

For example, see RFC 8594 .例如,请参阅RFC 8594

Hard part here is adoption.这里的困难部分是采用。

In a situation where you control both the client(s) and the server, adoption is much easier, since you can force the issue.在您同时控制客户端和服务器的情况下,采用会容易得多,因为您可以强制解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM