使用 or 条件定义查询参数的 RESTful 最佳实践是什么?

What are the RESTful best practices on defining a query parameter with an or condition?

我想知道,按照 RESTful 最佳实践,我应该以何种方式定义 url,如下所示:

/people?q="email=aa@aa.com||phone=11111"

重点是通过电子邮件地址或 phone 搜索某人,具体取决于他们在搜索框中插入的内容。

我一直在阅读一些关于使用 RESTful 服务的最佳实践的指南,但 none 似乎在谈论这种情况。

怎么样:

/people?condition=OR&email=aa@aa.com&phone=11111

这将允许在不更改端点的 name/URL 的情况下改进查询方法。

备选方案可能是:

/people?or_email=aa@aa.com&or_phone=11111

这样您还可以实现邮件搜索和phone

/people?and_email=aa@aa.com&and_phone=11111

甚至进行更复杂的查询,例如:

/people?and_email=aa@aa.com&and_phone=11111&or_name=Alice

REST 表示您通过构建(统一的)应用程序界面来遵循标准。

目前还没有通用GET查询的标准方案。您可以使用许多非标准 URL 查询语言。例如。 RQL, OData, most of the structure of database REST API URIs: e.g. mongodb, etc... So try not to reinvent the wheel. As an alternative you can send a serialized query in a query param as you did in your example. It can be on any standard query language, e.g. SPARQL if you use REST with RDF vocabs like hydra. If you don't then you have to describe it in the documentation of your vendor MIME type。 (在这种情况下,一般查询意味着您不知道查询的结构。)

如果您正在寻找非通用查询解决方案,那么您可以使用任何 URI template as long as you describe somehow the parameters of your link with your RDF vocab or in the documentation of your vendor MIME type. If you don't understand what I am talking about, check the uniform interface constraint / self-descriptive message, HATEOAS sections of the manual or use wikipedia。 (在此上下文中,非一般查询意味着您知道查询结构是什么,并且可以使用 URI 模板来描述它。)

请注意,您必须解析、验证和授权这些查询,因此在没有任何检查的情况下通过数据库发送 SQL 语句和 运行 它可能对您的应用程序是致命的。