What is RESTful way to query with filters?

Say that my application manages objects called workload, with the following fields. I want to open a REST interface for a user to request workloads using 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 create a REST endpoint so that it supports the load on requests with multiple labels as a filter?

Request example 1 : I want to GETload all workstations with both “A” and “B”.

I think something kind of GETlike a verb, workloadsas an endpoint, and then use {"labels": ["A", "B"]}as the body of the request. But this is not like RESTful way of doing things

As an alternative, I can do it GET /labels/{label-id}/workloads, but this will only work with one tag per time.

Request example 2 . I want to GETload all workloads labeled "A" or "B" but not "C"

There is no clue how to generally execute this type of api for relaxation, in addition to requesting a user a request for A, B, C separately, and then performing their own installation operations themselves?

The second query is tracked as another question

+2
source share
3 answers

GET . - "workload/labels/A, B, C". A, B, C . , , .

+1

, , .

GET /workloads?label=A&label=B&label=C

or not , .

GET /workloads?or_label=A&or_label=B&label_not=C
+1

, , A B. , ,

  • /workloads/?label=["A","B"]
  • /workloads/?label[]=A&label[]=B - .
  • /workloads/by/label/A+B/
  • /label/A+B/workloads/

There are also existing URI conventions, such as 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, As far as I know. There is no standard solution for describing complex URI filters.

0
source

Source: https://habr.com/ru/post/1655670/


All Articles