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

问题描述 投票:0回答:1

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

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);
});
node.js request async.js
1个回答
1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.