76304

What's a RESTful way to query with filters?

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"]}

<strong>Question</strong>: How do I design the REST endpoint so that it would supports query workload by multiple labels as filter?

<strong>Sample Query 1</strong>: 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.

<s><strong>Sample Query 2</strong>: 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?</s>

The second query is tracked as another question

Answer1:

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.

Answer2:

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

Answer3:

You have a lot of options the only constraint here, that they should contain A and B. So for example

    <li>/workloads/?label=["A","B"]</li> <li>/workloads/?label[]=A&label[]=B - aka. query string array</li> <li>/workloads/by/label/A+B/</li> <li>/label/A+B/workloads/</li> </ul>

    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.

Recommend

  • How to exclude a word from regex subpattern?
  • How tin-can-api works offline?
  • What is the file path, as a string, of a file in the application's resources?
  • How can I grant anonymous access to an httpHandler that uses wildcards in a site that requires authe
  • Java Jersey RESTful web service using https
  • Error in deploying code on heroku
  • ServiceStack, CORS, and OPTIONS (No Access-Control-Allow-Origin header)
  • Strange file-permissions if apps run as administrator
  • Logstash grok test with rspec has a different behavior?
  • Rails - Redundant RESTFUL Actions for map.resources? (new, create)
  • What if the best way to return Option types by WCF service
  • Flask-Restful error: “as_view” method not inherited
  • xcode don't localize specific strings
  • Reading a file into a multidimensional array
  • Android application: how to use the camera and grab the image bytes?
  • Alternative To body {overflow:scroll;} That Will Prevent Page Jostling/Wriggling?
  • Swift: Switch statement fallthrough behavior
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • Play WS (2.2.1): post/put large request
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • How to clear text inside text field when radio button is select
  • Scrapy recursive link crawler
  • java.lang.NoClassDefFoundError: com.parse.Parse$Configuration$Builder on below Lollipop versions
  • Spring security and special characters
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Symfony2: How to get request parameter
  • Display Images one by one with next and previous functionality
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • R: gsub and capture
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Comma separated Values
  • retrieve vertices with no linked edge in arangodb
  • SQL merge duplicate rows and join values that are different
  • log4net write single file for each call to log.info
  • Getting error when using KSoap library to consume .NET web services
  • Linking SubReports Without LinkChild/LinkMaster
  • How to Embed XSL into XML
  • Python/Django TangoWithDjango Models and Databases
  • How to load view controller without button in storyboard?