REST-查询字符串与URL路径 [英] REST - Query string vs. URL path

查看:138
本文介绍了REST-查询字符串与URL路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Web Api应用程序,目前正在努力理解RESTful API设计.

I am working on a Web Api application and I am currently struggling with my understanding of RESTful API Design.

假设有一个帖子资源/api/posts ,我希望客户有机会请求当月的所有帖子.据我了解,有两种方法可以实现这一目标.

Let's say there is a Post resource /api/posts and I want the clients give the opportunity to request all Posts for the current month. From my understanding there are 2 ways to achieve this.

一种方法是将当月作为资源.另一种方法可以是Post资源的查询字符串.

One way could be to make the current month as a resource. The other way could be a query string for the Post resource.

做到这一点的最佳方法(RESTful)是什么?

What is the best (RESTful) way to do this?

api/posts/currentmonth

api/posts?stardate = ???& enddate = ???

推荐答案

做到这一点的最佳方法(RESTful)是什么?

What is the best (RESTful) way to do this?

REST不在乎URI设计.

REST doesn't care about URI design.

URI设计准则主要旨在使人类更容易使用.通过使拼写更易于推理,或使路由实现更易于使用.

URI design guidelines intend primarily to make things easier for humans; either by making the spellings easier to reason about, or by making routing implementations easier to work with.

艾伦·贝茨(Alan Bates)对软件工程学的回答非常详尽.总结: RFC 3986 告诉我们层次结构属于路径,非层次结构数据属于路径查询.

Alan Bates answered this very thoroughly on softwareengineering. Summarizing: RFC 3986 tells us that hierarchy belongs in the path, non-hierarchical data belongs in the query.

一种方法是将当月作为资源.另一种方法可以是Post资源的查询字符串.

One way could be to make the current month as a resource. The other way could be a query string for the Post resource.

小心...

api/posts
api/posts?stardate=???&enddate=???

就REST和您的RESTful客户端而言,

这些资源是个不同的资源.查询字符串是标识符的一部分.

Those are different resources, as far as REST and your RESTful clients are concerned. The query string is part of the identifier.

用不同的方式说同一件事

Saying the same thing a different way

GET /api/posts?stardate=???&enddate=??? ...

表示

Resource(/api/posts?stardate=???&enddate=???).get()

不是

Resource(/api/posts).get(stardate=???&enddate=???)
Resource(/api/posts).query(stardate=???&enddate=???).get()

您的路由框架可以将前者转换为后者,但这是一个实现细节.REST旨在将这些细节与界面分开,以便客户端和服务器可以独立发展.

Your routing framework may translate the former into the one of the latter, but that's an implementation detail. REST is designed to separate these details from the interface, so that the clients and servers can evolve independently.

为什么是api/posts和api/posts?stardate = ???& enddate = ???不同的资源?我一直以为查询字符串只是一种查询资源的方法.

Why are api/posts and api/posts?stardate=???&enddate=??? different resources? I always thought that query strings are just a way to query on a resource.

我的理解是,这种观点有些过时.

My understanding is that view is somewhat dated.

RFC 1630 确实包含符合该解释的定义

RFC 1630 does include a definition that aligns with that interpretation

问号(?",ASCII 3F十六进制)用于界定可查询对象的URI与用来表示对该对象的查询的一组单词之间的边界.使用此格式时,组合的URI代表对象,该对象是通过将查询应用于原始对象而得出的.

The question mark ("?", ASCII 3F hex) is used to delimit the boundary between the URI of a queryable object, and a set of words used to express a query on that object. When this form is used, the combined URI stands for the object which results from the query being applied to the original object.

但是,在我们到达 RFC 3986

路径组件包含通常以层次结构形式组织的数据,以及非层次查询组件中的数据(

The path component contains data, usually organized in hierarchical form, that, along with data in the non-hierarchical query component (Section 3.4), serves to identify a resource within the scope of the URI's scheme and naming authority (if any).

重点已添加

如Fielding所述,资源的关键特征是它们是抽象的.

A key feature of resources, as described by Fielding, is that they are abstract.

REST使用资源标识符来标识组件之间交互中涉及的特定资源.REST连接器提供了用于访问和操作资源值集的通用接口,而与成员函数的定义方式或处理请求的软件类型无关.

REST uses a resource identifier to identify the particular resource involved in an interaction between components. REST connectors provide a generic interface for accessing and manipulating the value set of a resource, regardless of how the membership function is defined or the type of software that is handling the request.

换句话说,表示可以通过将查询传递给对象来生成,也可以通过在访问文档存储或高速缓存中的条目时使用标识符作为键来生成.客户端(和中间组件)不需要知道任何隐藏在接口后面的实现.他们只是确定他们感兴趣的概念映射,然后让服务器执行其任务.

In other words, the representation might be generated by passing a query to an object, or it might be generated by using the identifier as the key when accessing a document store -- or an entry in a cache. The client (and the intermediary components) don't need to know anything about it implementation hidden behind the interface; they just identify the conceptual mapping they are interested in, and let the server do its thing.

这篇关于REST-查询字符串与URL路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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