Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decoding https response data in node.js

Tags:

node.js

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?

like image 393
Vivek Mohan Avatar asked Jul 29 '12 10:07

Vivek Mohan


1 Answers

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.

like image 57
ebohlman Avatar answered Sep 30 '22 19:09

ebohlman