Using wildcards (*) in REST API resource name

12,401

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.

Share:
12,401
jlanza
Author by

jlanza

Updated on June 04, 2022

Comments

  • jlanza
    jlanza almost 2 years

    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 ;)