将一个API调用的响应传递给另一个API

编程入门 行业动态 更新时间:2024-10-09 03:31:12

将一个<a href=https://www.elefans.com/category/jswz/34/1771441.html style=API调用的响应传递给另一个API"/>

将一个API调用的响应传递给另一个API

我有两个网址,它们将添加新用户并按顺序编辑用户。 如何将第一个请求的输出传递给第二个请求作为输入。

http://localhost:3010/postuser
{"name":"xyz"}
// response will be unique id =>001

http://localhost:3010/putuser
{"id":001} //Get the output of first request as input here

下面是我的代码

function httpGet(options, callback) {
    request(options,
        function (err, res, body) {
            callback(err, body);
        }
    );
}
const urls = [
    {
        url: 'http://localhost:3010/postuser',
        method: 'POST'
    },
     {
        url: 'http://localhost:3010/putuser',
        method: 'PUT'
    }
];
async.mapSeries(urls, httpGet, function (err, res) {
    if (err) return console.log(err);
    console.log(res);
});
回答如下:

使用async.waterfall,以便可以将一个函数中的数据传递给下一个函数。 检查链接中的示例。 因此,基本上,您将调用httpGet函数并将使用回调从API1接收的数据传递给下一个函数。 然后,您可以调用API2。

更多推荐

将一个API调用的响应传递给另一个API

本文发布于:2024-05-06 19:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1753752.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!