避免使用REST进行重复的POST [英] Avoid duplicate POSTs with REST

查看:113
本文介绍了避免使用REST进行重复的POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在REST API中使用POST创建对象.服务器偶尔会创建对象,但是客户端将在收到201 Created响应之前断开连接.客户端仅看到失败的POST请求,然后稍后重试,服务器愉快地创建了一个重复的对象...

I have been using POST in a REST API to create objects. Every once in a while, the server will create the object, but the client will be disconnected before it receives the 201 Created response. The client only sees a failed POST request, and tries again later, and the server happily creates a duplicate object...

其他人一定有这个问题,对吧?但是我四处搜寻,每个人似乎都忽略了它.

Others must have had this problem, right? But I google around, and everyone just seems to ignore it.

我有2个解决方案:

A)改用PUT,然后在客户端上创建(GU)ID.

A) Use PUT instead, and create the (GU)ID on the client.

B)向在客户端上创建的所有对象添加一个GUID,并让服务器强制执行其UNIQUE -ness.

B) Add a GUID to all objects created on the client, and have the server enforce their UNIQUE-ness.

A不能很好地匹配现有框架,而B感觉就像是黑客.在现实世界中,其他人如何解决这个问题?

A doesn't match existing frameworks very well, and B feels like a hack. How does other people solve this, in the real world?

使用Backbone.js,可以在客户端上创建对象时将GUID设置为ID.保存后,Backbone将执行PUT请求.使您的REST后端句柄将PUT设置为不存在的ID,就可以设置了.

With Backbone.js, you can set a GUID as the id when you create an object on the client. When it is saved, Backbone will do a PUT request. Make your REST backend handle PUT to non-existing id's, and you're set.

推荐答案

我总是使用B -由于服务器端出现的任何问题,都检测到了dups.

I always use B -- detection of dups due to whatever problem belongs on the server side.

这篇关于避免使用REST进行重复的POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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