简体   繁体   English

如何在 REST API 中将多个 API 调用合并为一个调用

[英]How to merge multiple API calls into one call in REST API

For instance, if I have an API call http://localhost:3006/send-message?email=abc@mysite.com&password=xxxxxx&count=4&location=US and another API call http://localhost:3006/cancel-request?email=abc@mysite.com&password=xxxxxx&count=2&usernames=abc,xyz .例如,如果我有一个 API 调用http://localhost:3006/send-message?email=abc@mysite.com&password=xxxxxx&count=4&location=US和另一个 API 调用http://localhost:3006/cancel-request?email=abc@mysite.com&password=xxxxxx&count=2&usernames=abc,xyz So, can I merge these two calls into one by doing something like http://localhost:3006/send-message?email=abc@mysite.com&password=xxxxxx&count=4&location=US/cancel-request?count=2&usernames=abc,xyz .那么,我可以通过执行类似http://localhost:3006/send-message?email=abc@mysite.com&password=xxxxxx&count=4&location=US/cancel-request?count=2&usernames=abc,xyz类的操作将这两个调用合并为一个http://localhost:3006/send-message?email=abc@mysite.com&password=xxxxxx&count=4&location=US/cancel-request?count=2&usernames=abc,xyz If Yes then how I can handle this in Node.js with express.如果是,那么我如何使用 express 在 Node.js 中处理这个问题。

I'd suggest making these operations a POST instead of a GET.我建议将这些操作设为 POST 而不是 GET。 Then, you can have one URL for a multi-operation and you can have a JSON payload in the body that contains an array of operations and their parameters:然后,您可以拥有一个用于多操作的 URL,并且您可以在包含一组操作及其参数的主体中拥有一个 JSON 有效负载:

http://localhost:3006/multi-operation

With a JSON payload that parses to this:使用解析为的 JSON 有效负载:

[
   {
       operation: "send-message",
       email: "abc@mysite.com",
       password: "xxxxx",
       count: 4,
       location: "US"
   },
   {
       operation: "cancel-request",
       email: "abc@mysite.com",
       password: "xxxxx",
       count: 2,
       usernames: ["abc","xyz"]
   }
]

This would also only be sending sensitive information such as passwords in the body of the request which is generally considered safer than putting them in the URL (where they might get logged by various infrastructure).这也只会在请求正文中发送敏感信息,例如密码,这通常被认为比将它们放在 URL 中(它们可能会被各种基础设施记录)更安全。

Note: In a REST API design, a GET request is not supposed to have "side-effects".注意:在 REST API 设计中,GET 请求不应具有“副作用”。 It's supposed to retrieve some resource.它应该检索一些资源。 Calling it 0 times, 1 time or 10 times should have the same effect on the server/world.调用它 0 次、1 次或 10 次应该对服务器/世界具有相同的效果。 So, independent of the desire to specify multiple operations in one API call, neither of these operations should have been a GET operation anyway because they both have side effects (they cause some change to occur).因此,独立于在一个 API 调用中指定多个操作的愿望,这些操作都不应该是 GET 操作,因为它们都有副作用(它们会导致发生一些变化)。 So, these should likely be POST operations.因此,这些应该是 POST 操作。 There are lots of good articles on when to use GET, POST, PUT, PATCH, etc... in a REST API.有很多关于何时在 REST API 中使用 GET、POST、PUT、PATCH 等的好文章。 If you're confused by this, you can start with these articles or find many others with a search:如果您对此感到困惑,可以从这些文章开始或通过搜索找到许多其他文章:

https://restfulapi.net/http-methods/ https://restfulapi.net/http-methods/

https://www.restapitutorial.com/lessons/httpmethods.html https://www.restapitutorial.com/lessons/httpmethods.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM