编辑冲突和409与412的响应 [英] Edit conflicts and 409 vs. 412 responses

查看:246
本文介绍了编辑冲突和409与412的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个REST API是用来与应用程序后台工作。我们需要实现冲突prevention功能,这将在编辑请求(POST / PUT)将检查记录是不是客户端的它最后一次读取之间修改现在,如果是,就告诉客户有冲突。

We have a REST API which is used to work with the application backend. We need to implement a conflict prevention function, which would on edit request (POST/PUT) would check if the record was not modified between the client's last read of it and now, and if it were, would tell the client there's a conflict.

问题是如何发送冲突检查标签(这很可能将是一个时间戳,但我们不希望强制要求),以及如何返回错误。

The question is how to send the conflict check tag (which most probably would be a timestamp, but we don't want to mandate that) and how to return the error.

我们想使用标准的REST模式尽可能的,所以在这里我们考虑的解决方案:

We would like to use standard REST patterns as much as possible, so here the solutions we considered:


  • 使用如果-Modified-Since的。这里的问题在于,它强制要求使用时间戳,也是规范说你必须返回412我们想返回特定的409 code,以表明它是一个编辑冲突的as在spec 的描述,而不是更普通的412可由其他原因引起的。这也使它更容易为客户端有编辑冲突特殊处理,因为他们有专门的错误code。

  • Using If-Modified-Since. The problem here is that it mandates using a timestamp and also the spec says you MUST return 412. We'd like to return more specific 409 code, to indicate it is an edit conflict, as described in the spec, instead of much more generic 412 which can be caused by other reasons. This would also make it much easier for the client to have special handling for edit conflicts, because they would have dedicated error code.

使用如果-匹配即可。更好,因为我们可以利用连接到它的任何数据,但同样规格的授权使用,即使412 409适合我们的情况要好。此外,该规范提出如果-匹配是与ETag时,因为它是不可行的计算正确Etag的每一个记录,我们不使用ETag为我们的数据。我们有,我们将使用支票作为记录数据的一部分的标签,但它不被作为ETag的和现有的客户不处理ETag的,所以我们不希望如果可能的话强加给客户的这一新要求。

Using If-Match. Better since we can use any data attached to it, but again the spec mandates using 412 even though 409 fits our case better. Also, the spec suggests If-Match is linked to Etags, and we don't use Etags for our data because it's not feasible to calculate proper Etag for every record. We have the tag that we would be using for checks as part of the record data, but it is not sent as ETag and existing clients do not process ETags, so we wouldn't want to impose this new requirement on the clients if possible.

使用自定义X标头。这会工作得很好,将是pretty容易为客户添加,但是我们会尽可能使用标准的REST意味着preFER。

Using custom X-Header. This would work just fine and would be pretty easy for the clients to add but we'd prefer using standard REST means if possible.

那么,什么是在这种情况下推荐的方法是什么?有没有使用标准的REST手段,具有409响应,并拥有这一切非常干净的方法吗?

So, what is the recommended way in this case? Is there a way to use standard REST means, respond with 409 and have it all nice and clean?

推荐答案

基本上,如果你在头文件If_ *preconditions则必须返回412即使您使用自定义X标头,只有它也就是说,标题没有定义,它说,它必须返回412。如果它作为preconditions,如果你看一下412的定义,你应该用X标头的请求返回412。

Basically, if you have "If_*" preconditions in the header then you must return 412. Even if you use custom X-Header, it only means that header has no definition where it says it must return 412. If it's used as preconditions, if you look at 412 definition, you should return 412 for request with the X-Header.

 This response code allows the client to place preconditions on the current resource metainformation (header field data) ...

通常ETAG被要求只派犹如─的一部分* preconditions,所以如果你想409,那么你不会用太ETAG

Usually etag is only sent in requests as part of if-* preconditions, so if you want 409 then you won't use etag too.

如果你想使用409,那么只要把preconditions /在请求体后置不是头。通过WebDAV如果条件失败返回四百○九分之四百○三。 409当客户机可能能够修复请求。看到 http://www.ietf.org/rfc/rfc3253.txt

If you want to use 409, then just put the preconditions/postconditions in request body not header. Webdav returns 403/409 for if a condition failed. 409 when the client might be able to fix the request. see http://www.ietf.org/rfc/rfc3253.txt.

那么,就用412的头preconditions,否则409。

So, use 412 for preconditions in header, otherwise 409.

这篇关于编辑冲突和409与412的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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