I'm building RESTful API using Microsoft ASP.NET WebAPI. My problem concerns GET method, or more specifficaly, optimal solution for building query/filter strings in URL's.
In my application, RESTful API acts just as proxy, or access point to my resources stored in database. It is meant to retrieve, insert, modify or delete records. The whole system stores only one type of entity, which is pretty complex, because it contains many various fields, lists and data in general. I'm using NoSQL for storing those entities, as there is pretty many of them.
The problem lies in creating proper query string for retrieving those entities by specified criteria. Classic query string looks like this:
http://localhost/api/entities?field1=val1&field2=val2&field10=val3
What's wrong with this kind of query string is that it only allows to specify fields values by equality operator, and there is no place for logic operators between each field (& is treated as somewhat AND operator).
What I need, is to allow to specify more complex query strings, more like filters. I want to allow to tell system, that I want to get records, which have for instance:
field1 >= value1 OR field2=value2 AND field3 ~ value3
~ is indicates that i want to match field3 to value3 using FUZZY algorithm (although I'm not sure if ~ is proper character to indicate intention of using fuzzy search).
Well, as you can see, classic query string is not capable of supporting that. For now, I'm passing such query string as:
http://localhost/api/entities?query=field1>=value1$OR$field2=value2$AND$field3~value3
I pass such filter as one string to my controller's method and manually parse it. My delimiter is '$', as ASP.NET WebAPI would automatically split those parameters if there was '&' character between them, and I don't want it to do that, because it is unable map logical operators (OR, AND, etc.) to method parameters in such case. I'm not sure if I'm doing the right thing by parsing this string (everything after '?' character) manually:
field1>=value1$OR$field2=value2$AND$field3~value3
My question are:
- What is the proper way of representing such queries in RESTful API URLs?
- Is there any existing solution for supporting such filters in ASP.NET WebAPI, other than writing my own parsing algorithm?