Kinaan Khan Sherwani Kinaan Khan Sherwani - 3 months ago 13
HTML Question

nodejs - request.get(url) returning binary data in case of yahoo.com

I am trying to run the following javascript code.

request = require('request')
request.get('https://www.yahoo.com',function(err,res,body){ console.log(err ? err.message : body) })


But it is logging binary data whereas
html
string is expected.

The same code returns
html
response if I replace the first argument with a different url say
https://www.google.com
.

I have tried the same in ruby with following code

require('open-uri')
puts open('https://www.yahoo.com').read


It returns correct html response in both cases.

I think I am missing something while using
request
package of nodejs.

Answer

You just see gzip-encoded html string. Use gzip option (docs) to get decompressed body.

request.get({url: 'https://www.yahoo.com', gzip: true}, function(err,res,body){ console.log(err ? err.message : body) })