问题描述
说我的应用程序正在管理名为workload的对象,使用以下字段.我想公开一个REST接口,让用户通过标签查询工作负载.
"Workload": {"id":"test1", "labels":["A", "B", "C"]} "Workload": {"id":"test2", "labels":["A", "C", "D"]} "Workload": {"id":"test3", "labels":["A", "B", "D"]}
问题:如何设计REST端点,以便它将通过多个标签作为过滤器来支持查询工作负载?
样本查询1 :我想要GET与"a"和"b"的所有工作负载.
我认为像GET作为动词,workloads作为端点,然后使用{"labels": ["A", "B"]}作为请求主体.但这似乎并不像是一种善于做事的方式
或者,我可以做GET /labels/{label-id}/workloads,但这只能使用每次的一个标签.
没有clue如何做这种休息API,除了询问用户询问A,B,C分别是否正确设置操作本身?
第二查询是跟踪为另一个问题
推荐答案
get动词不接受请求正文.您应该做一些类似于'工作负载/标签/A,B,C'. 然后,您可以在请求查询中获取A,B,C.使用逗号分隔从请求查询并查找记录进行逗号.
其他推荐答案
使用查询参数,它的精细
GET /workloads?label=A&label=B&label=C
对于简单的案例,您还可以or和not这样的术语.
GET /workloads?or_label=A&or_label=B&label_not=C
其他推荐答案
您有很多选项唯一的约束,它们应该包含A和B.所以例如
- /workloads/?label=["A","B"]
- /workloads/?label[]=A&label[]=B - AKA. 查询字符串阵列
- /workloads/by/label/A+B/
- /label/A+B/workloads/
还有现有的URI查询约定,例如Microsoft Odata. http://docs.oasis-open.org/odata/odata/v4.0/odata-v4.0-part2-url-conventions.html 我不熟悉odata,但根据文档,您应该使用类似的东西/WorkloadsByLabels(labels=@c)?@c=["A","B"]如果您想遵循他们的方法.据我所知.没有标准解决方案来描述复杂的URI过滤器.
问题描述
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"]}
Question: How do I design the REST endpoint so that it would supports query workload by multiple labels as filter?
Sample Query 1: I want to GET all the workloads with both "A" and "B".
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
Alternatively, I can do GET /labels/{label-id}/workloads but this would only work with one label per time.
Sample Query 2: I want to GET all the workloads with label "A" or "B" but no "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?
The second query is tracked as another question
推荐答案
GET verb not takes request body. You should do something like 'workload /labels/A, B, C '. You then get A,B, C in request query. Make an array with comma separated from request query and find records.
其他推荐答案
Use query parameters, its fine to repeat them.
GET /workloads?label=A&label=B&label=C
For simple cases you could alsoor and not the terms like this.
GET /workloads?or_label=A&or_label=B&label_not=C
其他推荐答案
You have a lot of options the only constraint here, that they should contain A and B. So for example
- /workloads/?label=["A","B"]
- /workloads/?label[]=A&label[]=B - aka. query string array
- /workloads/by/label/A+B/
- /label/A+B/workloads/
There are existing URI query conventions as well, for example Microsoft Odata. http://docs.oasis-open.org/odata/odata/v4.0/odata-v4.0-part2-url-conventions.html I am not familiar with OData, but according to the docs you should use something like /WorkloadsByLabels(labels=@c)?@c=["A","B"] if you want to follow their approach. Afaik. there is no standard solution to describe complex URI filters.