叶中楷 叶中楷 - 1 year ago 51
reST (reStructuredText) Question

Shall restful response include or exclude empty result?

Here is a api that use a batch of user id to get the users

GET /user/{user_id}

user_id : comma separated string

e.g. http://user.example.com/user/1,2,3

Now I have 2 users in id 1 and 2, but no user for id 3.

And shall the response include the empty user or exclude?

Include example:

{
"id":"1",
"name":"Chris",
"age":29
},
{
"id":"2",
"name":"Max",
"age":30
},
{
"id":"3"
}


Exclude example:

{
"id":"1",
"name":"Chris",
"age":29
},
{
"id":"2",
"name":"Max",
"age":30
}

Answer Source

If there's no resource with the given identifier, it must not be included in the response.

If you have a collection of users, try to identify it with a plural noun (/users instead of /user) and use a query string parameter to filter it, such as /users?ids=1,2,3.

When using JSON to represent a collection, use an array (which begins with [ and ends with ] and the values are separated by ,).

For the status code when requesting a representation of a collection or a single resource, refer to this answer.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download