返回对象中项目总数的最佳 RESTful 方法是什么? [英] What’s the best RESTful method to return total number of items in an object?

查看:18
本文介绍了返回对象中项目总数的最佳 RESTful 方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我参与的大型社交网站开发 REST API 服务.到目前为止,它运行良好.我可以向对象 URL 发出 GETPOSTPUTDELETE 请求并影响我的数据.但是,此数据是分页的(一次限制为 30 个结果).

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

但是,通过我的 API 获取会员总数的最佳 RESTful 方式是什么?

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

目前,我向 URL 结构发出请求,如下所示:

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

  • /api/members—返回成员列表(如上所述,每次 30 个)
  • /api/members/1—影响单个成员,具体取决于所使用的请求方法
  • /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

我的问题是:我将如何使用类似的 URL 结构来获取应用程序中的成员总数?显然,仅请求 id 字段(类似于 Facebook 的 Graph API)并计算结果是无效的,因为只会返回 30 个结果的切片.

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.

推荐答案

虽然对/API/users 的响应被分页并只返回 30 条记录,但没有什么可以阻止您在响应中包括记录总数,以及其他相关信息,例如页面大小、页码/偏移量等.

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.

StackOverflow API 是相同设计的一个很好的例子.这是用户方法的文档 - https://api.stackexchange.com/docs/users

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

这篇关于返回对象中项目总数的最佳 RESTful 方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆