3

I guess this is probably a simple question, but I am seeing conflicting answers out there.

I am building a restful web api service that returns class objects. In addition to being able to request ALL items, one item, or category like so:

http://MyService/Albums

http://MyService/Albums/12345

http://MyService/Category/Blues

I also want to be able to give the users the ability to select multiple, such as:

http://MyService/Albums/12345,77777,87654

http://MyService/Category/Blues,Pop,Metal

I don't need the syntax shown above, that was just an example.

I've already accomplished this by creating code like so:

[Route("Albums/Category/{categoryList}")]
public List<Album> GetAlbumsByCategory(string categoryList)
{
  int[] cats = categoryList.Split(',');
  return(AlbumManager.GetAlbums(cats));
}

This works fine, but the user needs to know that the input can either be a single value, or a list of values by comma. Then in my AlbumManager I am iterating through the album collection and adding only the selected items to the new list.

I want to know:

(A) Is there already shortcuts in REST and preferred ways of doing this? I have seen some recommendations of Category=Blues&Category=Pop& ..... Seems verbose to me, but if there is a standard out there I want to use it.

(B) When I use my approach, I'm constantly writing split code all over the place. Is there a shortcut in REST for this to simplify? I have seen some [FromUri] references but it's not clear to me.

enforge
  • 915
  • 3
  • 10
  • 26

1 Answers1

0

a). Rest is a little relaxed on a precise recommended way to do this, basically your have 3 options. None of which are any more "RESTful" that the other, choose the one which best suites your API user - which may be different than what suites your self, after-all API users are users too :-).

1). Implement a custom ActionFilter which will save you from the split code, see the multiple answers over on this question https://stackoverflow.com/a/19107738/1422377

2). As "against principle" as this sounds, send a POST request that accepts a List from the body. Yes a purest could argue that unless you are modifying data this should not constitute a post, but remember the user.

3). Modify your current approach slightly to make it more clear to the API user what is expected to them as input. for example:

public List<Album> GetAlbumsByCategory([FromUri] string[] categoryList)
{
  //your code here
}

Personally, in your case I would go with option 3 - although verbose the syntax is going to be familiar to any API user, followed by option 1. If you had a more complex input model go with option 2. As I said none are really any more RESTful than the other.

Community
  • 1
  • 1
BMac
  • 2,183
  • 3
  • 22
  • 30
  • Thanks for your answer. If I am reading it right, it sounds like although my approach is not incorrect and REST isn't super strict, it does not fall into one of the 3 recommended approaches. I think I prefer 3, even though as you said it is verbose. Last concern/question, what if they want to query for thousands of items (that is a real scenario for me)? I supposed they would either have to break it up themselves into query string length friendly batches for GET, or I would have to use a POST approach. – enforge Apr 29 '15 at 19:26
  • 1
    @enforge if you need support for thousands you will be forced to use post, else you may hit into the upper limit of a GET request url as described http://stackoverflow.com/a/2659995/1422377 – BMac Apr 29 '15 at 22:39