What's the best RESTful way to pass collections of strings as parameters?

I have a rails app with RESTful-ish urls where I need to pass a collection of strings (tags) and I don't want to use a query string.

I am currently using a route similar to /controller/tagged/:tags/foo/:foo/bar/:bar.:format

This requires coding "tags" which is a pain and error if you want to manually enter a URL.

Other approaches, such as / controller / tagged / tag 1 / tag2 / tag3 / foo ..., are tricky in that they are ambiguous (it is not clear where the end of the tag list ends and other parameters start).

How do other people approach this sort of thing, and what is your solution to express it as a track of rails?

+2


source to share


4 answers


Let's say you are trying a GET

resource, perhaps a "List of Questions", and you want to filter the list based on a set of "Tags" (which is a great idea!).

Your URL must be a resource path. Maybe a /questions

good url. I know you don't want to use url parameters for your tags, but in GET, what they are for is to filter or customize the resource view. So to filter multiple questions of interest, rather than get all questions, your url might look like this:



/questions?tag=ruby-on-rails&tag=restful&tag=url-routing

      

I believe this is the RESTful way. The alternatives may be acceptable, but I think they would violate the "purist" form of REST.

+4


source


Use a query string. It's there for exactly this purpose: passing encoded arguments to a resource.

Your version using path elements hides the fact that it /controller/tagged

is the actual resource being requested, with tags as parameters for the request.

To understand why the path method is not RESTful, consider the following two URLs:



http://example.com/controller/tagged/foo/bar

http://example.com/controller/tagged/bar/foo

According to the description you provided, they must refer to the same virtual collection of tagged items. However, in a RESTful system, URLs uniquely refer to one and only one resource. Your addressing links multiple URLs with a single result.

+2


source


Sam Ruby and Leonard Richardson recommend in their book Restful Web Services to separate non-hierarchical data with commas or semicolons (and query variables for algorithmic resources).

+1


source


Okay, you could always STATE them.

-1


source







All Articles