1

I am using the express module in node.js and I am trying to read the body of an HTTP GET request and send an answer back to the user based on the content of the body. I am new to node.js and this is not working for me.

I know I should be using HTTP POST for this (and it works when I am using post), but the server I am trying to mimic in node.js uses GET, so I have to stick to it. Is there a way to do this in node.js? Thanks!

Here is a sample code I did. When I use app.post, I see the logs for the body, but when I use app.get, I see no logs at all!

app.get('/someURL/', function(req, res){

  var postData = '';

  req.on('data', function(chunk) {
    console.log("chunk request:");
    console.log(chunk);
    postData += chunk;
  });

  req.on('end', function() {
    console.log("body request:");
    console.log(postData);
  });

  //Manipulate response based on postData information

  var bodyResponse = "some data based on request body"

  res.setHeader('Content-Type', 'application/json;charset=utf-8');
  res.setHeader('Content-Length', bodyResponse.length);

  res.send(bodyResponse);
};
Karl-Johan Sjögren
  • 14,076
  • 7
  • 55
  • 62
Maxime Marchand
  • 341
  • 1
  • 12

1 Answers1

1

The version of Node's HTML parser you are using does not support bodies in GET requests. This feature is available on the latest unstable v0.11 branch.

OrangeDog
  • 30,151
  • 11
  • 105
  • 177
  • Hi, Thanks for the reply, but I just tried using node v0.11.11 and I encountered the same issue for both the windows and linux version of node.js. Basically, the req.on('data',fucntion(chunk){}; does not pickup the body of the GET Request. Any ideas? – Maxime Marchand Jul 14 '14 at 17:37