6

While playing with https in node.js, I have stucked in reading response data. Following is the code for https request;

https.get(options, function(resp) {                     
    console.log(resp.headers)        //working fine
    resp.on('data', function(d) {           
        console.log(d)             // buffered data; like <Buffer 7b 22 69...
        process.stdout.write(d);  // working fine(prints decoded data in console)
        var decoded_data=???    }); 
}).on('error', function(e) {
    console.error(e);
});

But, how can I decode response data & write it into a variable?

1 Answer 1

14
var decoded_data = d.toString('utf8');

or, earlier on:

resp.setEncoding('utf8');

and then all your on events will give you a string instead of a buffer.

Sign up to request clarification or add additional context in comments.

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.