简体   繁体   English

你如何通过ID以外的东西获取RESTful资源?

[英]How do you GET a RESTful resource by something other than ID?

There may be situations where I would need to find an object by parameters other than ID. 在某些情况下,我需要通过除ID之外的参数来查找对象。 What is the proper RESTful way to do that? 什么是正确的RESTful方式呢?

For example I might want to find a User by username and password , so the strictly RESTful " GET /users/1 " wouldn't work. 例如,我可能想要通过usernamepassword找到User ,因此严格的RESTful“ GET /users/1 ”将无效。

According to the Rails docs this is the URL pattern for getting all the instances of a resource: " GET /users ". 根据Rails文档,这是获取资源的所有实例的URL模式:“ GET /users ”。 I could add parameters to that: " GET /users?username=joe&password=topsecret ", but that would be perverting the official purpose of the GET request. 我可以添加参数:“ GET /users?username=joe&password=topsecret ”,但这会违反GET请求的官方目的。

"GET /users?username=joe&password=topsecret", but that would be perverting the official purpose of the GET request." “GET / users?username = joe&password = topsecret”,但这会歪曲GET请求的官方目的。“

No it isn't perverting anything. 不,它不会变态。 That's absolutely the correct and RESTful way to do it, and is the reccomended way of retrieving dynamic results in the http spec. 这绝对是正确且RESTful的方式,并且是在http规范中检索动态结果的推荐方式。 REST doesn't care what's in the URL, only that it's unique. REST不关心URL中的内容,只关注它是唯一的。 The url for that page could be http://3f778a9b8a7c778696e for all REST architecture cares, so long as that's the only way to get there, and it doesn't ever lead anywhere else. 对于所有REST架构关注,该页面的URL可以是http:// 3f778a9b8a7c778696e ,只要这是实现目标的唯一方法,并且它不会在其他任何地方引导。

http defines a query string protocol for returning dynamic results. http定义用于返回动态结果的查询字符串协议。 Given the current state of your database, the query string that you give your application ought to always return the same result. 给定数据库的当前状态,您为应用程序提供的查询字符串应始终返回相同的结果。 Then it will be RESTFUL. 然后它将是RESTFUL。 URL aesthetics are a different issue from REST altogether. URL美学与REST完全不同。

according to the REST architecture, the rules of the GET request are that it always returns the same results (or maintains the same results for reasonably long periods of time, so that caching works), and that GET Doesn't have side effects. 根据REST架构,GET请求的规则是它总是返回相同的结果(或者在相当长的时间内保持相同的结果,以便缓存起作用),并且GET没有副作用。 GET needs to be idempotent (always return the same results, regardless of how many times you call it) and not cause the system to change state. GET需要是​​幂等的(总是返回相同的结果,无论你调用多少次)并且不会导致系统改变状态。 That is it . 就是这样

Of course you don't have to use the query protocol. 当然,您不必使用查询协议。 You can put parameters into forward slashes, inbetween semicolons, or it could be a base64 encoded GUID. 您可以将参数放在正斜杠中,在分号之间,或者它可以是base64编码的GUID。 It's entirely up to you, as long as it follows those simple rules. 这完全取决于你,只要遵循这些简单的规则。

Multiple keys is not restful, is it? 多把钥匙不安宁,是吗? Perhaps /users/[username]?password=secret . 也许/users/[username]?password=secret Also, you don't want to use the password there, but some kind of API key, so that an url looks like this: 此外,您不想在那里使用密码,而是使用某种API密钥,以便网址如下所示:

/users/leethal?secret=da01930adfe82810092

In order to use the username instead of the id, do this: 要使用用户名而不是id,请执行以下操作:

# controller
@user = User.find_by_username!(params[:id])

# model
def to_param
  username
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM