如何将请求响应传递给另一个请求?
在ExpressJS应用程序中使用request-promise模块,我想提出两个请求,但是我需要从第一个请求中得到的响应数据传递给第二个请求。
我之后的一个例子是,
const options = { url: 'http://api.example.com/v1/token', method: 'GET' }; request(options).then((response) => { request({ url: 'http://api.example.com/v1/user', method: 'POST', data: { token: response.token } }).then((final_response) => { res.send(final_response); }); });
我已经省略了error handling以保持示例简短。 我的兴趣是将一个请求的响应传递给另一个请求的技术。
你可以通过返回它们来链接承诺。 就像是:
request(options1) .then((response1) => { return request(options2) }) .then((response2) => { return request(options3) }) .then((final_response) => { res.send(final_response); });
这是一个关于promise链接和error handling的好文章。