最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

网站源码admin16浏览0评论

将一个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。

发布评论

评论列表(0)

  1. 暂无评论