0

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();
}

http://nodejs.org/api/stream.html#stream_event_data

2 Answers 2

2

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);
});
Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.