What’s the best RESTful method to return total number of items in an object?

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.


    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 - https://api.stackexchange.com/docs/users


    I prefer using HTTP Headers for this kind of contextual information.

    For total number of elements I use X-total-count header.
    For links to next, previous page etc. I use http Link header:
    http://www.w3.org/wiki/LinkHeader

    Github does it same way: https://developer.github.com/v3/#pagination

    In my opinion it's more clean since it can be used also when you return content which doesn't support hyperlinks (ie binaries, pictures).


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

    链接地址: http://www.djcxy.com/p/40972.html

    上一篇: cv2.aruco.detectMarkers在python中不检测标记

    下一篇: 什么是返回对象中项目总数的最佳RESTful方法?