Quantcast
Channel: Where is the body in a nodejs http.get response? - Stack Overflow
Viewing all articles
Browse latest Browse all 24

Answer by nkron for Where is the body in a nodejs http.get response?

$
0
0

The data event is fired multiple times with 'chunks' of the body as they are downloaded and an end event when all chunks have been downloaded.

With Node supporting Promises now, I created a simple wrapper to return the concatenated chunks through a Promise:

const httpGet = url => {  return new Promise((resolve, reject) => {    http.get(url, res => {      res.setEncoding('utf8');      let body = '';       res.on('data', chunk => body += chunk);      res.on('end', () => resolve(body));    }).on('error', reject);  });};

You can call it from an async function with:

const body = await httpGet('http://www.somesite.com');

Viewing all articles
Browse latest Browse all 24

Trending Articles