RESTful 系统中一个请求的多条记录 [英] Multiple records with one request in RESTful system

查看:40
本文介绍了RESTful 系统中一个请求的多条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过的所有关于 RESTful 架构的例子都处理了一条记录.例如,对 mydomain.com/foo/53 的 GET 请求以获取 foo 53 或对 mydomain.com/foo 的 POST 以创建新的 Foo.

All the examples I've seen regarding a RESTful architecture have dealt with a single record. For example, a GET request to mydomain.com/foo/53 to get foo 53 or a POST to mydomain.com/foo to create a new Foo.

但是多条记录呢?能够通过 id 请求一系列 Foos 或发布一组新的 Foos 通常对于单个 API 请求会比几十个单独的请求更有效.您会重载"mydomain.com/foo 来处理对单个或多个记录的请求吗?或者你会添加一个 mydomain.com/foo-multiple 来处理多个 POST 和 GET 吗?

But what about multiple records? Being able to request a series of Foos by id or post an array of new Foos generally would be more efficient with a single API request rather than dozens of individual requests. Would you "overload" mydomain.com/foo to handle requests for both a single or multiple records? Or would you add a mydomain.com/foo-multiple to handle plural POSTs and GETs?

我正在设计一个可能需要一次获取许多记录的系统(类似于 mydomain.com/foo/53,54,66,86,87)但是因为我没有没有看到任何这样的例子,我想知道是否有什么我只是不了解 RESTful 架构的东西使这种方法错误".

I'm designing a system that may potentially need to get many records at once (something akin to mydomain.com/foo/53,54,66,86,87) But since I haven't seen any examples of this, I'm wondering if there's something I'm just not getting about a RESTful architecture that makes this approach "wrong".

推荐答案

虽然我看不出你的方法有什么本质上的错误,但你的 API 用户想要 foo 具有任意 ID 的记录.我立即怀疑他们正在使用 ID 来表示不同的概念,例如,上周创建的所有 foo 等.如果这就是他们正在做的,那么您可能应该在服务器上公开该功能,而不是使用 ID 列表作为代理.

While I don't see anything intrinsically wrong with your approach, it seems a little odd for a user of your API to want foo records with arbitrary IDs. I'd immediately suspect that they are using the IDs to represent a different concept, e.g., all the foos that were created in the last week, etc. If that's what they're doing, then you should probably expose that functionality on the server instead of using the list of IDs as a surrogate.

这篇关于RESTful 系统中一个请求的多条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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