2016-11-29 43 views
0

我有兩個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); 
}); 
+0

如果有人知道答案,請發表您的評論或回答,而不是向下投票 – user4324324

回答

1

使用async.waterfall所以從一個功能可將數據傳遞到下一個功能。檢查link中的示例。所以基本上你會調用httpGet函數,並使用回調將從API1接收到的數據傳遞給下一個函數。然後你可以調用API2。

相關問題