开发者

What if I want to implement a complicated query in REST

开发者 https://www.devze.com 2022-12-17 03:04 出处:网络
I would like to implement a REST service which is able to parse query such as retrieving the users created after startdate and before endate and with the privilege of admin. It seems that s开发者_Stac

I would like to implement a REST service which is able to parse query such as retrieving the users created after startdate and before endate and with the privilege of admin. It seems that s开发者_StackOverflowtandard REST implementation could only query by ID. Do I need to self-defined protocol to make this kind of query possible or any standard?

Thanks!


There is no constraint in REST that says you can only query by ID. There is absolutely nothing wrong with using a set of query parameters to return a set of users that match those search criteria.


Take a look at Google's GDATA Protocol. It's very RESTful and they have a very nice way of performing "complex" queries while still keeping a clean URI.

http://code.google.com/apis/gdata/docs/2.0/reference.html#Queries

Here is an example of what their clean query URIs

http://example.com/jo/-/Fritz/2006

instead of

http://example.com/jo?category=Fritz&category=2006

From Google:

This approach identifies a resource without using query parameters, and it produces cleaner URIs. We chose this approach for categories because we think that category queries will be the most common queries.


As far as i'm concerned - restful stuff is valid for CRUD actions. You can implement separate search method/url and pass it as many parameters as you need. And if you still try to adhere to REST methodologies - you should use GET request type.


Ryan Bates has a Railscast where he shows creating searches as a resource. The concept is still valid, even if you're not doing Rails.

http://railscasts.com/episodes/111-advanced-search-form


I take the field lookups semantics of django as the guidelines for complex lookups. https://docs.djangoproject.com/en/dev/ref/models/querysets/#id4

Don't be sidetracked by the fact that it is defined as a lanuage-specific api, it is easy to translate to html query parameters.

An example would be http://example.com/Goods?category=fruit&size__lt=14

To get the result of select * from goods where category = 'fruit' and size < 14 in sql or /goods[@category='fruit' and @size < 14] in xpath, or whatever.

0

精彩评论

暂无评论...
验证码 换一张
取 消