I'm using the bluebird library and need to make a series of HTTP requests and need to some of the response data to the next HTTP request. I've built a function that handles my requests called callhttp(). This takes a url and the body of a POST.
I'm calling it like this:
var payload = '{"Username": "joe", "Password": "password"}';
var join = Promise.join;
join(
callhttp("172.16.28.200", payload),
callhttp("172.16.28.200", payload),
callhttp("172.16.28.200", payload),
function (first, second, third) {
console.log([first, second, third]);
});
The first request gets an API key which needs to be passed to the second request and so on. How do get the response data from the first request?
UPDATE
This is the callhttp function:
var Promise = require("bluebird");
var Request = Promise.promisify(require('request'));
function callhttp(host, body) {
var options = {
url: 'https://' + host + '/api/authorize',
method: "POST",
headers: {
'content-type': 'application/json'
},
body: body,
strictSSL: false
};
return Request(options).spread(function (response) {
if (response.statusCode == 200) {
// console.log(body)
console.log(response.connection.getPeerCertificate().subject.CN)
return {
data: response.body
};
} else {
// Just an example, 200 is not the only successful code
throw new Error("HTTP Error: " + response.statusCode );
}
});
}