When I use response.on('data' callback)
I get the data as soon as it is received. This results in the data getting passed part by part.
I wish to get the data all together and call the parser.parseString(chunk,parseData)
. How do I achieve this?
The following is my code.
var request = https.request(options, function(response) {
response.setEncoding('utf8');
response.on('data', function (chunk) {
console.log('BODY: ', chunk);
//parser.parseString(chunk,parseData);
});
response.on('end', function () {
console.log('The end');
});
});
request.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
request.write(str);
request.end();
}
You should create variable, where you'll be writing document data
var document;
response.on('data', function(chunk) {
document += chunk;
}
Then you should trigger your parser at end of document
response.on('end', function() {
parser.parseString(document, parseData);
});
If you consider or are using the Express framework, see if the body-parser middleware meets your need (c.f. https://github.com/expressjs/body-parser)