Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using wildcards (*) in REST API resource name

Is it sensible to use * in REST API for a resource ID? I want to use it for searching. I'm using RESTEasy for developing my webservice.

Suppose I have resources that are user and user has Name and Age. Then my REST API looks like:

/users/{id}/name
/users/{id}/age

Now if I want to display all names I'm thinking on using the following:

/users/*/name

Is this correct or should I use another way of doing?

Edit 1: Adding subresources

As from an answer it is suggested to use a fields query param. But let's suppose I'm now want something that is a property of the sub-resource. For instance:

/user/*/name/full
/user/*/name/short

If I follow the fields option, I will have to do:

/user?fields=name-short 
/user?fields=name-full 

Which it is not nice as the properties of name are linked to the name class somehow.

Please do consider the example as that. Try to get the idea ;)

like image 664
jlanza Avatar asked Oct 17 '22 05:10

jlanza


1 Answers

Query parameters

You can probably use wildcards, but not in the way you've shown in your question. What if you want to get more details besides the name in the same request?

You could use a query parameter to allow field selection for the users collection:

/users?fields=name
/users?fields=age
/users?fields=name,age

Or you can get some inspiration from how partial responses are handled in the Google Drive API:

/users?fields=name(short,full)

Alternatively, you could use dot notation:

/users?fields=name.short,name.full

Or use a mix of both, like in the Spotify API.

Custom media types

If you don't need field selection, you can use custom media type, returning a predefined set of fields.

like image 101
cassiomolin Avatar answered Oct 21 '22 07:10

cassiomolin