REST API可以至少以两种方式拥有参数:

作为url路径的一部分(即/api/resource/parametervalue) 作为查询参数(例如/api/resource?=参数值)

这里的最佳实践是什么?什么时候使用1,什么时候使用2,有什么一般的指导方针吗?

真实世界的例子:Twitter使用查询参数来指定间隔。(http://api.twitter.com/1/statuses/home_timeline.json?since_id=12345&max_id=54321)

把这些参数放在URL路径中会被认为是更好的设计吗?


当前回答

根据URI标准,路径用于层次参数,查询用于非层次参数。离岸金融中心。对你来说什么是等级制度是非常主观的。

在将多个uri分配给相同资源的情况下,我喜欢将用于标识的参数放入路径中,将用于构建表示的参数放入查询中。(对我来说,这种方式更容易路由。)

例如:

/users/123和/users/123?字段= "姓名、年龄" /users和/users?name = " John "年龄= 30

对于map reduce,我喜欢使用以下方法:

/用户?name = " John "年龄= 30 /用户/名称:约翰/年龄:30岁

因此,如何构造uri实际上取决于您(以及您的服务器端路由器)。

注意:只是提到这些参数是查询参数。因此,您真正要做的是定义一种简单的查询语言。对于复杂查询(包含与、或、大于等操作符),我建议您使用已经存在的查询语言。URI模板的功能非常有限…

其他回答

根据universe-resource-locator提供的“上下文”“打包”和POST数据,这对于定位器来说意味着#1。

注意第二条的局限性。比起第一条,我更喜欢post。

注:此处讨论的局限性

POST参数内容是否有最大大小?

GET请求的长度有限制吗?和最大大小的URL参数在_GET

附注:这些限制是基于客户端功能(浏览器)和服务器(配置)。

这是个很有趣的问题。

你可以同时使用它们,关于这个主题没有任何严格的规则,但是使用URI路径变量有一些优点:

缓存: 大多数互联网上的web缓存服务不缓存GET请求时,他们包含查询参数。 他们这样做是因为有很多RPC系统使用GET请求来更改服务器中的数据(失败!!)Get必须是一个安全的方法)

但是如果使用路径变量,所有这些服务都可以缓存GET请求。

层次结构: 路径变量可以表示层次结构: /城市/街道/的地方

它为用户提供了关于数据结构的更多信息。

但如果你的数据没有任何层次关系,你仍然可以使用Path变量,使用逗号或分号:

/城市/经度,纬度

作为规则,当参数的顺序重要时使用逗号,当顺序不重要时使用分号:

/ IconGenerator /红色,蓝色,绿色

除了这些原因之外,在某些情况下,使用查询字符串变量是非常常见的:

当您需要浏览器自动将HTML表单变量放入URI时 当你处理算法的时候。例如谷歌引擎使用查询字符串:

http:// www.google.com/search ? q =休息

总而言之,没有任何强烈的理由使用这种方法,但只要可以,就使用URI变量。

There are no hard and fast rules, but the rule of thumb from a purely conceptual standpoint that I like to use can briefly be summed up like this: a URI path (by definition) represents a resource and query parameters are essentially modifiers on that resource. So far that likely doesn't help... With a REST API you have the major methods of acting upon a single resource using GET, PUT, and DELETE . Therefore whether something should be represented in the path or as a parameter can be reduced to whether those methods make sense for the representation in question. Would you reasonably PUT something at that path and would it be semantically sound to do so? You could of course PUT something just about anywhere and bend the back-end to handle it, but you should be PUTing what amounts to a representation of the actual resource and not some needlessly contextualized version of it. For collections the same can be done with POST. If you wanted to add to a particular collection what would be a URL that makes sense to POST to.

这仍然留下了一些灰色区域,因为一些路径可以指向多少子代的父资源,这在某种程度上是自由裁量的,取决于他们的使用。这所画出的一条界限是,任何类型的传递表示都应该使用查询参数来完成,因为它不会有底层资源。

为了响应原始问题中给出的真实示例(Twitter的API),参数表示一个可传递查询,它过滤资源的状态(而不是层次结构)。在那个特定的例子中,向由这些约束表示的集合中添加数据是完全不合理的,而且该查询将无法表示为在对象图中有任何意义的路径。

The adoption of this type of resource oriented perspective can easily map directly to the object graph of your domain model and drive the logic of your API to the point where everything works very cleanly and in a fairly self-documenting way once it snaps into clarity. The concept can also be made clearer by stepping away from systems that use traditional URL routing mapped on to a normally ill-fitting data model (i.e. an RDBMS). Apache Sling would certainly be a good place to start. The concept of object traversal dispatch in a system like Zope also provides a clearer analog.

以下是我的观点。

查询参数被用作请求的元数据。它们充当现有资源调用的过滤器或修饰符。

例子:

/ / 2014-08-08 /日历事件

应该给出当天的日历事件。

如果需要特定类别的事件

/日历/ 2014-08-08 /事件吗?类别=约会

或者你需要超过30分钟的比赛

/日历/ 2014-08-08 /事件吗?时间= 30

试金石测试是检查在没有查询参数的情况下是否仍然可以提供请求。

我看到很多REST api不能很好地处理参数。经常出现的一个例子是URI包含个人身份信息。

http://software.danielwatrous.com/design-principles-for-rest-apis/

我认为一个必然的问题是,当一个参数根本不应该是一个参数,而应该被移动到请求的HEADER或BODY。