What saying good practice about GET resource

98 Views Asked by At

I wonder what saying good practices create REST API. Should or not I create URI which will allow get e.g. several specific users?

For example I mean:

/usermanagement/users/{j_goldman,wafik,morder}

And this uri will be returns 3 objects users for j_goldman, wafik and morder.

2

There are 2 best solutions below

2
On

You can do this but it won't be restful IMHO. If you really need to do this you should think about remodeling your resource selections say, all three users you want to get belong to a particular group with id 111. Then you can do something like /groups/111 GET. If you cannot then I guess you should stick with restful solution and use three API calls to get users separately.

2
On

What you are doing is searching for a specific set of users. With a query parameter within your URL, you can achieve this.

To return a single user (id is 5):

/usermanagement/users/5

To return all users:

/usermanagement/users

To return a set of users based on search:

/usermanagement/users?username=

That way, your API is open to searching by a specified criteria which can also be extended.

Say, you wish to search by location:

/usermanagement/users?location=

Say, you wish to combine these:

/usermanagement/users?username={criteria}&location={criteria}

You may also want to expose a search endpoint itself:

/usermanagement/search

You might find other options here too:

RESTful URL design for search