Web API / REST: Request List of Items
I think this is probably a simple question, but I see conflicting answers there.
I am creating a soothing api web service that returns class objects. In addition to being able to request ALL items, one item or category, for example:
http: // MyService / Albums / 12345
http: // MyService / Category / Blues
I also want users to be able to select multiple, for example:
http: // MyService / Albums / 12345,77777,87654
http: // MyService / Category / Blues, Pop, Metal
I don't need the syntax shown above, this was just an example.
I've already done this by creating code like this:
[Route("Albums/Category/{categoryList}")]
public List<Album> GetAlbumsByCategory(string categoryList)
{
int[] cats = categoryList.Split(',');
return(AlbumManager.GetAlbums(cats));
}
This works great, but the user should be aware that the input can be either a single value or a comma-separated list of values. Then in my AlbumManager, I iterate over the collection of albums and add only the selected items to the new list.
I want to know:
(A) Are there shortcuts in REST and preferred ways to do this? I've seen some guidelines Category = Blues & Category = Pop & ..... Sounds verbose to me, but if there is a standard I want to use it.
(B) When I use my approach, I am constantly writing separate code all over the place. Is there a shortcut in REST for simplicity? I've seen some [FromUri] links, but it's not clear to me.
source to share
and). The rest is a bit relaxed with the exact recommended way to do it, basically you have 3 options. Neither one is more "RESTful" and the other chooses the one that suits your API user best - which may be different from what suits your self as well as API users for all users :-).
1). Implement a custom ActionFilter that will save you the splitting code, see multiple answers on this fooobar.com/questions/25809 / ...
2). As "against principle" as it sounds, send a POST request that takes a list from the body. Yes, the cleanest might be to argue that if you don't change the data it shouldn't constitute a message, but remember the user.
3). Modify your current approach slightly to make the API user more understandable what is expected as input. eg:
public List<Album> GetAlbumsByCategory([FromUri] string[] categoryList)
{
//your code here
}
Personally, in your case, I would go with option 3 - although the verbose syntax would 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 of them anymore is not RESTful than the other.
source to share