2

I try to send a request to an Apache server and use the returned body.

Follow the manual of nodejs http://nodejs.org/api/https.html#https_https_request_options_callback

and related SO content How to make external HTTP requests with Node.js

My src is

callback = function(response) {
  body='';
  console.log('HEADERS: ' + JSON.stringify(response.headers));
  response.on('data', function (chunk) {
    body+= chunk;
  }); 

  response.on('end', function () {
    console.log(body);
  }); 
}

My question is that the body here will include all the HTML header tag, such as <!DOCTYPE html>...etc, which cannot be parsed by JSON object. Because all my data in the HTML body is JSOn, I want to get only the HTML body. Are there anyway to achieve this goal? Thanks in advance.

Community
  • 1
  • 1
StevenR
  • 702
  • 1
  • 8
  • 17

1 Answers1

4

I am not giving complete code here.

  1. You can use modules like htmlparser2 to extract the data from HTML's body (Online demo for the same).

  2. And then you can use JSON.parse to parse the extracted string to a JSON Object.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497