views:

67

answers:

4

I'm developing a REST API service for a large social networking website I'm involved in. So far, it's working great. I can issue GET, POST, PUT and DELETE requests to object URLs and affect my data. However, this data is paged (limited to 30 results at a time).

However, what would be the best RESTful way to get the total number of say, members, via my API?

Currently, I issue requests to a URL structure like the following:

/api/members - Returns a list of members (30 at a time as mentioned above)

/api/members/1 - Affects a single member, depending on request method used

My question is, how would I then use a similar URL structure to get the total number of members in my application? Obviously requesting just the id field (similar to Facebook's Graph API) and counting the results would be ineffective given only a slice of 30 results would only be returned.

Thanks in advance.

+1  A: 

What about a new end point > /api/members/count which just calls Members.Count() and returns the result

Steve Woods
Cheers. Think I'll go with this new endpoint approach.
Martin Bean
Giving the count an explicit endpoint makes it a standalone addressable resource. It will work, but will raise interesting questions for anybody new to your API - Is the count of the collection members a separate resource from the collection? Can I update it with a PUT request? Does it exist for an empty collection or only if there are items in it? If the `members` collection can be created by a POST request to `/api`, will `/api/members/count` be created as a side effect as well, or do I have to do an explicit POST request to create it before requesting it? :-)
Franci Penov
A: 

Seems easiest to just add a

GET
/api/members/count

and return the total count of members

willcodejavaforfood
+5  A: 

While the response to /API/users is paged and returns only 30, records, there's nothing preventing you from including in the response also the total number of records, and other relevant info, like the page size, the page number/offset, etc.

The StackOverflow API is a good example of that same design. Here's the documentation for the Users method - http://api.stackoverflow.com/1.0/help/method?method=users

Franci Penov
Good idea. Every list returned should specify the total count as well. Otherwise, how would you know if there was a need to fetch the next page?
bzlm
+1: Definitely the most RESTful thing to do if fetch limits are going to be imposed at all.
Donal Fellows
@bzim You would know there is a next page to fetch because there is a link with rel="next".
Darrel Miller
@Darrel: Which in turn requires that the receiving code understand the word “next”. ;-)
Donal Fellows
@Darrel Miller - `<link rel="next">` would work only for HTML payloads. if the resource is represented in application/json, that won't work.
Franci Penov
@Franci Well it could work for pretty much an xml based media type. And it doesn't take a lot of imagination to figure out a way to encode links into JSON. Plenty of people are doing it.
Darrel Miller
@Donal the "next" rel is registered with IANA http://www.iana.org/assignments/link-relations/link-relations.txt
Darrel Miller
@Darrel - yes, it could be done with any type of "next" flag in the payload. I just feel that having the total count of the collection items in the response is valuable by itself and works as a "next" flag just the same.
Franci Penov
A: 

You could return the count as a custom HTTP header in response to a HEAD request. This way, if a client only wants the count, you don't need to return the actual list, and there's no need for an additional URL.

(Or, if you're in a controlled environment from endpoint to endpoint, you could use a custom HTTP verb such as COUNT.)

bzlm
“Custom HTTP header”? That would come under the heading of being somewhat surprising, which in turn is contrary to what I think a RESTful API should be. Ultimately, it should be unsurprising.
Donal Fellows
@Donal I know. But all the good answers were already taken. :(
bzlm
@bzlm: I know too, but sometimes you've just got to let other people do the answering. Or make your contribution better in other ways, such as a detailed explanation of why it should be done the best way rather than others.
Donal Fellows