I'm doing an http.request and in case of a server response error 500, I would like to access the data the was sent during the request in order to retry the request. How do I access the requestBodyData parameter from the response callback scope?
see below:
var arrayofobjects = [
{
"name" : "peter",
"age" : "24"
},
{
"name" : "gleice",
"age" : "19"
},
{
"name" : "pedro",
"age" : "34"
}
];
for(var x=0; x<arrayofobjects.length; x++){
sendModelInputData(arrayofobjects[x]);
}
function sendModelInputData (requestBodyData) {
const req = https.request(connectionInfo, (res) => {
logger.info(`STATUS: ${res.statusCode}`)
logger.debug(`HEADERS: ${JSON.stringify(res.headers)}`)
//how to access requestBodyData from here????
res.setEncoding('utf8');
res.on('data', (chunk) => {
logger.info(`BODY: ${chunk}`);
});
res.on('end', () => {
//console.log('No more data in response.');
});
});
req.on('error', (e) => {
//console.error(`problem with request: ${e.message}`);
logger.error(`problem with request: ${e.message}`);
});
//req.end(postData);
req.end(JSON.stringify(requestBodyData));
}