将一个API调用的响应传递给另一个API
问题描述:
我有两个URL会添加新用户并按顺序编辑用户。如何将第一个请求的输出传递给第二个请求作为输入。将一个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所以从一个功能可将数据传递到下一个功能。检查link中的示例。所以基本上你会调用httpGet函数,并使用回调将从API1接收到的数据传递给下一个函数。然后你可以调用API2。
如果有人知道答案,请发表您的评论或回答,而不是向下投票 – user4324324