Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RESTful URL design - how to query using OR between parameters

Tags:

rest

How would you design a RESTful query to support OR operand between parameters. Let's say my resource has two fields field1 & field2. How would you design the URL to enable the following query:

"Get myresources where field1=x OR field2=y"

Designing queries in REST is pretty straight forward, but I have only seen queries that supports AND between query fields. e.g. /myresource?field1=x&field2=y

A possible solution can be to provide a single query parameter with free text where part, for example:

GET /myresource?q={field1=x OR field2=y}

But that would make it more complicated for clients to parse and extend or reduce filtered fields.

What do you suggest?

like image 340
LiorH Avatar asked Jan 30 '09 12:01

LiorH


2 Answers

Query params aren't by definition AND related, they're just inert params - how you handle them is up to you. For an OR search I'd suggest:

GET /myresources?field1=x&field2=y&inclusive=true

If you want to default to an AND relationship (reasonable), and any other extension you want is of course possible.

like image 198
annakata Avatar answered Oct 13 '22 10:10

annakata


For what its worth, SO uses the following format for finding questions with multiple tags:

http://stackoverflow.com/questions/tagged?tagnames=jquery or css or asp.net or php or web-development or svn

It's perfectly reasonable to separate them with , or ; assuming those aren't valid characters for the tags themselves. Search engines typically use q=keyword1+keyword2 and url-encode any + in the keywords themselves which is what I would suggest you do if this is for a search URI.

like image 43
aleemb Avatar answered Oct 13 '22 08:10

aleemb