2016-11-29 12 views
0

私は新しいユーザーを追加してユーザーを順番に編集する2つのURLを持っています。最初の要求の出力を2番目の要求に入力として渡すにはどうすればよいですか。以下は1つの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を呼び出すことができます。

関連する問題