1

I am designing a rest api where users can pass in queries using a search query language I will define.
The language will allow a number of operators eq, ne, gt, lt (equals, not equals, greater than, less than) etc etc.

The language will allow grouping and logical operators AND and OR.

So for example a query about companies may look like the following

/api/companies?q=(CompanyName eq Microsoft Or CompanyName eq Apple) And State eq California

So this should give me all companies where company name equals 'Microsoft' or 'Apple' and the state is California.

So this all works fine except for the fact that the system that I am writing the api against is extremely flexible and allows almost any character to be inserted into fields values. Additionally, I also must support custom fields and those are able to have special characters in the field name.

Initially my main concern was fields that contained parentheses. I will be converting this query into a SQL server query and I need a way to ensure that I do not confuse a parentheses in a field value with one that is intended for grouping. My second thought was to force field values to be quoted, but I think this will also cause similar problems.

I was also considering that there may be a simple approach involving html encoding, but I am unable to see exactly how that would work.

What I am looking for is any advice or examples of reasonable approaches to handle a rest search query with such flexible data.

Ronnie
  • 670
  • 6
  • 15

2 Answers2

0

You should use percent encoding to escape characters in your query string, see RFC 3986. This previous StackOverflow post contains some useful background information about URI encoding.

Initially my main concern was fields that contained parentheses. I will be converting this query into a SQL server query and I need a way to ensure that I do not confuse a parentheses in a field value with one that is intended for grouping

If this might be a problem then it sounds like your application will be susceptible to SQL injection. You should be escaping any external data before constructing an SQL query.

/api/companies?q=(CompanyName eq Microsoft Or CompanyName eq Apple) And State eq California

Based on this example you could take advantage of the URI query string to better represent your query:

/api/companies?CompanyName=Microsoft%20OR%20Apple&State=California
Community
  • 1
  • 1
robert_b_clarke
  • 1,463
  • 10
  • 13
0

Here is an example. http://www.sqlservercentral.com/articles/Full-Text+Search+(2008)/64248/

suing
  • 2,808
  • 2
  • 16
  • 18