RESTful - GET 或 POST - 该怎么做? [英] RESTful - GET or POST - what to do?

查看:59
本文介绍了RESTful - GET 或 POST - 该怎么做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个我想成为 RESTful 的网络服务.我知道 CRUD 的做事方式,但我有一些我不完全清楚的事情.所以是这样的:

Im working on a web service that i want to be RESTful. I know about the CRUD way of doing things, but I have a few things that im not completly clear with. So this is the case:

我有一个跟踪服务,它在浏览器(客户端)中收集一些数据,然后将其发送到跟踪服务器.有两种情况,一种配置文件存在,另一种不存在.最后,服务返回一些必须注入到 DOM 的元素.

I have a tracking service that collects some data in the browser (client) and then sends it off to the tracking server. There are 2 cases, one where the profile exists and one where it does not. Finally the service returns some elements that has to be injected to the DOM.

所以基本上我需要 2 个网络服务:

So basically i need 2 web services:

  1. http://mydomain.tld/profiles/
  2. http://mydomain.tld/elements/

问题 1:现在我只使用 GET,但我重写了服务器以支持 CRUD.所以在这种情况下,如果配置文件不存在,我必须使用 POST.像 http://mydomain.tld/profiles/ 之类的东西,然后 POST 有效负载有要保存的信息.如果配置文件存在,我使用 PUT 和 http://mydomain.tld/profiles//和 PUT 的负载有数据要保存.一切都很好,但问题是据我所知,xmlhttp 不支持 PUT.现在可以使用 POST 吗,即使它是一个更新?

Question 1: Right now im only using GET, but im rewriting the server to support CRUD. So in that case i have to use POST if the profile does not exist. Something like http://mydomain.tld/profiles/ and then POST payload have the information to save. If the profile is existing i use PUT and http://mydomain.tld/profiles// and payload of PUT has data to save. All good, but problem is that as far as i understand, xmlhttp does not support PUT. Now is it ok to use POST even though its an update?

问题 2:如上所述,当制作曲目时,我的服务会返回一些要注入到 DOM 中的元素.从逻辑上讲,为了保持 RESTful,我想我必须使用 POST/PUT 来更新配置文件,然后使用 GET 来获取要注入的元素.但是为了节省服务器端的带宽和资源,将带有 POST/PUT 的元素返回到配置文件更有意义,即使它是不同的资源.您对此有何看法?

Question 2: As said my service returns some elements to be injected into the DOM, when a track is made. Logically, to keep it RESTful, i guess that i would have to use POST/PUT to update the profile and then GET to get the elements to inject. But to save bandwidth and resources on the serverside, it makes more sense to return the elements with the POST/PUT to profiles, even though its a different resource. What are your take on this?

BR/Sune

问题 3:在某些情况下,我只想更新配置文件而不接收返回元素.我是否仍然可以使用相同的资源,然后使用有效载荷参数来指定我是否需要元素,例如dont_receive_elements:真"

Question 3: In some cases i only want to update the profile and NOT receive back elements. Could i still use same resource and then using a payload parameter to specify if i want elements, e.g. "dont_receive_elements:true"

推荐答案

关于问题 #1,您确定 xmlhttp 不支持put"吗?我刚刚在三个浏览器上运行 http://www.mnot.net/javascript/xmlhttprequest/(Chrome、Firefox、IE),根据输出,put"在所有浏览器上都成功.关注http://www.slideshare.net/apigee/rest-design-webinar(我强烈建议您查看有关 Restful API 的许多 Apigee 视频和幻灯片),建议将put"用于您提到的用例.

On question #1, are you sure that xmlhttp does not support "put"? I just ran http://www.mnot.net/javascript/xmlhttprequest/ on three browsers (Chrome, Firefox, IE) and according to the output, "put" was successful on all browsers. Following the information on http://www.slideshare.net/apigee/rest-design-webinar (and I highly recommend checking out the many Apigee videos and slideshows on restful API), "put" is recommended for the use case you mention.

但是,您可以通过对数据的一些不同思考来完全避免这个问题.是否可以考虑您有一个配置文件,并且对于每个配置文件,您有 0 组或多组有效载荷信息?在这个模型中,两种情况是:1.不存在个人资料,在.../profiles/上创建带有POST的个人资料然后将带有帖子的元素/跟踪数据添加到.../profile/123/tracks/(或.../profile/123/elements/)2.配置文件存在,只需添加元素/跟踪数据

But you may be able to avoid this issue entirely by thinking a little differently about your data. Is it possible to consider that you have a profile and that for each profile you have 0 or more sets of payload information? In this model the two cases are: 1. No profile exists, create profile with a POST on .../profiles/ Then add elements/tracking data with posts to .../profile/123/tracks/ (or .../profile/123/elements/) 2. Profile exists, just add the elements/tracking data

(抱歉没有详细了解您的模型,很难做到非常精确).

(Sorry without understanding your model in detail, it is hard to be very precise).

至于问题 #2 - 使用配置文件具有 0 个或多个元素的数据模型,您可以更新配置文件(添加必要的元素),然后返回更新的配置文件(及其完整的元素图),保存你有任何额外的收获.

As for question #2 - going with a data model where a profile has 0 or more elements, you could update the profile (adding the necessary elements) and then return the updated profile (and its full graph of elements), saving you any additional gets.

更笼统地说,关于问题 2,作为 API 的开发人员,您在 REST 世界中有相当多的自由 - 如果您专注于让 API 的使用者简单明了,那么您可能没问题.

More generally on question #2, as the developer of the API you have a fair amount of freedom in the REST world - if you are focused on making it easy and obvious for the consumers of your API then you are probably fine.

底线:查看 www.apigee.com - 他们知道的比我多得多.

Bottom line: Check out www.apigee.com - they know much more than I.

这篇关于RESTful - GET 或 POST - 该怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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