在剩下的是POST或PUT最适合UPSERT操作? [英] In REST is POST or PUT best suited for upsert operation?

查看:144
本文介绍了在剩下的是POST或PUT最适合UPSERT操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把键 - 值存储在服务器的客户端。如果用户发送键K1,然后我把它UPSERT到数据库。这被认为是POST或PUT。

I keep key-value storage in the server for the client. If the user send key "k1" then I upsert it to the database. Is this considered POST or PUT.

我也有另一种操作,它删除所有现有密钥,并添加新的关键,这是POST或PUT,因为它明确记录,并添加新的。

Also I have another operation which remove all existing keys and add the new key, is this POST or PUT because it clear records and add new one.

推荐答案

按照 HTTP规范

这是封装的实体提供的Request-URI下储存的PUT方法请求。如果Request-URI指向一个已经存在的资源,封闭实体应该被视为驻留在源服务器上的一个的修改后的版本。如果Request-URI没有指向一个现有的资源,并且该URI是能够被定义为通过该请求的用户代理的新资源,源服务器可以创建该URI的资源。

The PUT method requests that the enclosed entity be stored under the supplied Request-URI. If the Request-URI refers to an already existing resource, the enclosed entity SHOULD be considered as a modified version of the one residing on the origin server. If the Request-URI does not point to an existing resource, and that URI is capable of being defined as a new resource by the requesting user agent, the origin server can create the resource with that URI.

因此​​,我认为,对于插入或更新使用PUT是完全合法的,但在两种情况下URI预先知道。如果你使用的密钥作为URI的一部分(如K1在 http://www.somewhere.com/resources/k1 < /一>),这应该是这样的。能够完美RESTful的,然而,一个GET到相同的URL也应该让你下载的资源。

I therefore think that the use of PUT for an insert or update is perfectly legitimate, provided that in both cases the URI is known in advance. If you're using the key as part of the URI (as k1 in http://www.somewhere.com/resources/k1) this should be the case. To be ideally RESTful, however, a GET to the same URL should also allow you to download the resource.

我不觉得,因为它做了两件事这种操作可以考虑REST风格。这似乎是提供一个宏,以满足特定的客户端,而不是简单地访问数据的需要。一个标准的REST风格的设计是

I don't think this operation could be considered RESTful because it does two things. It seems to be providing a macro to satisfy the needs of a particular client, rather than simple access to data. A standard RESTful design would be


  1. 通过发送一个GET到父代URL获取密钥的列表。在上面的例子中,这将是 http://www.somewhere.com/resources ;

  2. 通过发送删除对 http://www.somewhere.com/resources/k1 删除每个键

  3. 通过发送一个PUT到 http://www.somewhere.com/resources/k2 添加更换。

  1. Getting a list of keys by sending a GET to the parent URL. In the example above, that would be http://www.somewhere.com/resources;
  2. Deleting each of those keys by sending a DELETE to http://www.somewhere.com/resources/k1;
  3. Adding the replacement by sending a PUT to http://www.somewhere.com/resources/k2.

这是不太清楚,但我认为这也将是合法的通过发送一个DELETE请求 HTTP删除所有资源:// WWW .somewhere.com /资源

It's less clear cut, but I think it would also be legitimate to delete all resources by sending a single DELETE request to http://www.somewhere.com/resources.

这篇关于在剩下的是POST或PUT最适合UPSERT操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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