I have the following API: /api/v1/artworks/tagged?tag[]=, I'd like to be able to do /api/v1/artworks/tagged?tag=painting as well as /api/v1/artworks/tagged?tag[]=painting&tag[]=sculpture. So something like this:
params do
requires :tag, types: [String, Array[String]], desc: "Tag or tags."
end
Maybe instead I could write this:
params do
optional :tag, type: String, desc: "Tag or tags."
optional :tag, type: Array[String], desc: "Tag or tags."
exactly_one_of :tag
end
That would also solve the problem of allowing a parameter like size to be a number (eg. 10) or a string, (eg. "all").
I don't know if it's a normal/default thing, but I saw in some APIs the developers are using comma to separate the values.
Something like that: `/api/v1/artworks/tagged?tag=1,2,3,4
Is that a pattern?
The Rack/Rails style is tag[]=1&tag[]=2&tag[]=3, etc.
Yes, but is Rack/Rails style a good pattern for API endpoints?
That's the spring/Java style too, fwiw.
Sent from my phone
On Oct 13, 2014, at 8:42 AM, Daniel Doubrovkine (dB.) @dblockdotorg [email protected] wrote:
The Rack/Rails style is tag[]=1&tag[]=2&tag[]=3, etc.
—
Reply to this email directly or view it on GitHub.
I personally think it's good, because building array separator logic into the value is always problematic. Here you have a clear name/value and encoding pattern.
This can also be approached with making tag only an Array[String]:
params do
optional :tag, type: Array[String], desc: "Tag or tags."
end
get :tagged do
p params[:tag] #=> this is always an array
end
This will accept both /tagged?tag=painting and /tagged?tag[]=painting&tag[]=sculpture, except that grape validator will also convert the former to an array with single element. So you can expect the value of tag to always be an array in your models.
Most helpful comment
Yes, but is Rack/Rails style a good pattern for API endpoints?