您如何通过 ID 以外的其他方式获取 RESTful 资源? [英] How do you GET a RESTful resource by something other than ID?

查看:40
本文介绍了您如何通过 ID 以外的其他方式获取 RESTful 资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些情况下,我可能需要通过 ID 以外的参数来查找对象.什么是正确的 RESTful 方式来做到这一点?

There may be situations where I would need to find an object by parameters other than ID. What is the proper RESTful way to do that?

例如我可能想通过 usernamepassword 找到一个 User,所以严格的 RESTful "GET/users/1" 不起作用.

For example I might want to find a User by username and password, so the strictly RESTful "GET /users/1" wouldn't work.

根据 Rails 文档,这是获取所有实例的 URL 模式资源:GET/users".我可以向其中添加参数:GET/users?username=joe&password=topsecret",但这会破坏 GET 请求的官方目的.

According to the Rails docs this is the URL pattern for getting all the instances of a resource: "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."

不,它并没有歪曲任何东西.这绝对是正确的 RESTful 方式,也是在 http 规范中检索动态结果的推荐方式.REST 不关心 URL 中的内容,只关心它是唯一的.该页面的 url 可以是 http://3f778a9b8a7c778696e,适用于所有 REST 架构问题,只要这是到达那里的唯一途径,它永远不会领先于其他任何地方.

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. REST doesn't care what's in the URL, only that it's unique. 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.

http 定义了一个用于返回动态结果的查询字符串协议.给定数据库的当前状态,您提供给应用程序的查询字符串应该始终返回相同的结果.然后它将是RESTFUL.URL 美学与 REST 完全不同.

http defines a query string protocol for returning dynamic results. 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. URL aesthetics are a different issue from REST altogether.

根据 REST 架构,GET 请求的规则是它总是返回相同的结果(或者在相当长的时间内保持相同的结果,以便缓存工作),并且 GET 没有边效果.GET 需要是幂等的(无论调用多少次,始终返回相同的结果)并且不会导致系统更改状态.那就是.

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. 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. That is it.

当然你不必使用查询协议.您可以将参数放入正斜杠、分号之间,或者它可以是 base64 编码的 GUID.只要遵循这些简单的规则,这完全取决于您.

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. It's entirely up to you, as long as it follows those simple rules.

这篇关于您如何通过 ID 以外的其他方式获取 RESTful 资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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