开发者

Rails query string parameter format when providing an API with filters

开发者 https://www.devze.com 2023-03-27 14:53 出处:网络
I\'m opening up a few REST API calls to others to use. Some of them include search filters. Let\'s say I have a grocery endpoint When I currently make calls I might use:

I'm opening up a few REST API calls to others to use. Some of them include search filters.

Let's say I have a grocery endpoint When I currently make calls I might use:

/grocery_items/index.json?types[]=fruit&types[]=deli

Leaving me with params[:types] as a nice collection.

However to make things easier for the folks consumi开发者_StackOverflow中文版ng my API I want to be able to offer something like this:

/grocery_items/index.json?types=fruit,deli

Seems trivial to just split my params into a collection but I'm wondering if there are pitfalls since this seems to be against the grain of how rails expects collections to arrive as params.


I don't see anything wrong with doing a quick params[:types].split(',') to make calling your API easier to use. It's pretty common to do tricks with the query string, and this is a really tame change.

0

精彩评论

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

关注公众号