Home > Enterprise >  Can I get the full http response text in nodejs from a http module .get response?
Can I get the full http response text in nodejs from a http module .get response?

Time:12-05

I have a very simple webserver:

const ws = require('http');

ws.createServer(
    function(req,res)
    {
        console.log('request received');
        res.write('Hello world');
        res.end();
    })
    .listen(1234);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Server works. When I open browser on localhost:1234, I get Hello World text. When I send a GET to localhost:1234 from REST client, I get:

HTTP/1.1 200 OK
Date: Fri, 03 Dec 2021 20:10:12 GMT
Connection: close
Transfer-Encoding: chunked

Hello world

Now, I would like to write a test, but I'm having trouble finding any way to extract the "Hello world" text out of the response. At the moment, my test code looks like this:

const http = require('http');

let req = http.get('http://localhost:1234',(res)=>{
    let txt = res.read();
    console.log(txt);
});
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

This always returns null.

I've also tried:

const http = require('http');

let req = http.get('http://localhost:1234',(res)=>{
    let data = [];
    res.on('data',(chunk)=>data.push(chunk));
    console.log(data);
});
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

This returns an empty array.

When I debug and look at the res object, it's easy enough to find everything in the response (mostly in res.headers) except the Hello World text. I've seen lots of examples to extract data that is sent in JSON format etc, but I want to start with the simplest possible example (just plain text) and can't seem to find any way of doing this. Seems like anything the browser can return should be available when I do a get via the http module, but I just can't find it.

CodePudding user response:

you can read the response as json by doing this way :

Server file :

const ws = require('http');

ws.createServer(
  (request, response) => {

    console.log('request received');

    response.write('{"body": "Hello World !"}');
    response.end();

  }
).listen(1234);

Request file :

const http = require('http');

http.get('http://localhost:1234', (response) => {

  response.on("data", (chunk) => {

    body = JSON.parse(chunk).body

    console.log(body); // return 'Hello World !' as text
  })

}).on('error', function(e) {
  console.log("Error message : "   e.message);
});

CodePudding user response:

I managed to figure it out. Well sort of. I found something that works, but still don't completely understand the mechanics of it.

The following works and logs

Hello world

to the console.

const http = require('http');
let req = http.get('http://localhost:1234',async (res)=>{
    res.on('readable', () => {
        let txt=res.read().toString();
        console.log(txt);
    });
});

Seems like you need to listen for the readable event in order to get anything out of read() and when you do this does bring back the Hello world text (as a Buffer array though, so ToString() is needed).

Similarly, the following also works for the 'data' event:

const http = require('http');

let req = http.get('http://localhost:1234',(res)=>{
    let txt = '';
    res.on('data',(chunk)=>{
        txt =chunk.toString();
        console.log(txt);
    });
});

I have some followup questions, but I'll do some more research and ask again in new question(s) if I don't find clarity.

  • Related