问题描述
有关我要创建一个RESTful API,我需要有一些功能,获得的资源,但如果它不存在,创建它,然后返回它。我不认为这应该是一个GET请求的默认行为。我可以启用某个参数我给予GET请求这一功能,但似乎有点脏。
For a RESTful API that I'm creating, I need to have some functionality that get's a resource, but if it doesn't exist, creates it and then returns it. I don't think this should be the default behaviour of a GET request. I could enable this functionality on a certain parameter I give to the GET request, but it seems a little bit dirty.
主要的一点是,我想要做的只是这个要求,因为这些请求会被从潜在有一个缓慢的互联网连接的移动设备完成的,所以我想限制需要被尽可能多完成的请求越好。
The main point is that I want to do only one request for this, as these requests are gonna be done from mobile devices that potentially have a slow internet connection, so I want to limit the requests that need to be done as much as possible.
我不知道这是否适合在REST风格的世界,但如果它不,它会令我失望,因为这将意味着我必须做出对REST的想法一点点黑客。
I'm not sure if this fits in the RESTful world, but if it doesn't, it will disappoint me, because it will mean I have to make a little hack on the REST idea.
有谁知道这样做,否则,一个beatiful方式的RESTful的方式,不与REST理念的冲突?
Does anyone know of a RESTful way of doing this, or otherwise, a beatiful way that doesn't conflict with the REST idea?
推荐答案
客户是否需要提供任何资料作为创建的一部分?如果是这样,那么你真的需要,否则分离出GET和POSTas您需要发送的信息与每个GET,这将是非常难看。
Does the client need to provide any information as part of the creation? If so then you really need to separate out GET and POSTas otherwise you need to send that information with each GET and that will be very ugly.
如果不是要发送GET无需任何额外的信息,则没有理由,如果它已经不前返回它存在后端无法创建资源。根据的时间量需要创建你可能要考虑一下去异步和使用202按其他的答案的资源,但是那意味着你的客户端来处理(还)另一响应code,所以可能离开只是在等待资源完成并返回更好。
If instead you are sending a GET without any additional information then there's no reason why the backend can't create the resource if it doesn't already exist prior to returning it. Depending on the amount of time it takes to create the resource you might want to think about going asynchronous and using 202 as per other answers, but that then means that your client has to handle (yet) another response code so it might be better off just waiting for the resource to be finalised and returned.
这篇关于获取资源,但创造它,如果不存在它RESTful方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!