什么是使用过滤器查询的 RESTful 方式? [英] What's a RESTful way to query with filters?
问题描述
假设我的应用程序正在管理称为工作负载的对象,其中包含以下字段.我想公开一个 REST 接口供用户通过标签查询工作负载.
Say my application is managing objects called workload, with the following fields. I want to expose a REST interface for user to query workloads by labels.
"Workload": {"id":"test1", "labels":["A", "B", "C"]}
"Workload": {"id":"test2", "labels":["A", "C", "D"]}
"Workload": {"id":"test3", "labels":["A", "B", "D"]}
问题:我如何设计 REST 端点,以便通过多个标签作为过滤器来支持查询工作负载?
Question: How do I design the REST endpoint so that it would supports query workload by multiple labels as filter?
示例查询 1:我想GET
同时包含A"和B"的所有工作负载.
Sample Query 1: I want to GET
all the workloads with both "A" and "B".
我想像 GET
作为动词,workloads
作为端点,然后使用 {"labels": ["A", "B"]}
作为请求正文.但这似乎不是一种 RESTful 做事方式
I'm thinking something like GET
as verb, workloads
as endpoint, then use a {"labels": ["A", "B"]}
as request body. But this does not seem like a RESTful way to do things
或者,我可以执行 GET/labels/{label-id}/workloads
但这一次只能处理一个标签.
Alternatively, I can do GET /labels/{label-id}/workloads
but this would only work with one label per time.
示例查询 2:我想GET
标签为A"或B"但没有C"的所有工作负载
Sample Query 2: I want to GET
all the workloads with label "A" or "B" but no "C"
完全不知道怎么做这种rest api,除了让用户分别按A、B、C查询然后自己做适当的设置操作?
No clue how to do this sort of rest api at all, other than ask user to query by A, B, C separately then do proper set operations themselves?
第二个查询被作为另一个问题跟踪
推荐答案
使用查询参数,很好到
GET /workloads?label=A&label=B&label=C
对于简单的情况,您也可以or
和not
像这样的术语.
For simple cases you could alsoor
and not
the terms like this.
GET /workloads?or_label=A&or_label=B&label_not=C
这篇关于什么是使用过滤器查询的 RESTful 方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!