title: How to pass response paramaters including body from POST http.request in NodeJS
body: <p>I am trying to write a basic REST Post client to work with node.js and because of the REST API I have to work with I have to get details from the responses including cookies to maintain the state of my REST session with the server. My Question is what is the best way to pull the json objects from the response when res.on triggers with all the data in the PRINTME variable and return it to the test.js console.log().</p>
  
  <h2>test.js file</h2>
  
  <pre><code>var rest = require('./rest');
  rest.request('http','google.com','/upload','data\n');
  console.log('PRINTME='JSON.stringify(res.PRINTME));
  </code></pre>
  
  <h2>rest.js module</h2>
  
  <pre><code>exports.request = function (protocol, host, path, data, cookie){
  var protocalTypes = {
      http: {
          module: require('http')
          , port: '80'
      }
      , https: {
          module: require('https')
          , port: '443'
      }
  };
  
  var protocolModule = protocalTypes[protocol].module;
  
  var options = {
      host: host,
      port: protocalTypes[protocol].port,
      path: path,
      method: 'POST',
      headers: {
          'Content-Type': 'text/xml'
          , 'Content-Length': Buffer.byteLength(data)
          , 'Cookie': cookie||''
      }
  };
  
  console.log('cookies sent= '+options.headers.Cookie)
  
  var req = protocolModule.request(options, function(res) {
      var PRINTME = res;
      console.log('STATUS: ' + res.statusCode);
      console.log('HEADERS: ' + JSON.stringify(res.headers));
      res.setEncoding('utf8');
      res.on('data', function (chunk) {
          PRINTME.body = chunk;
          console.log('BODY: ' + chunk);
      });
      res.on('close', function () {res.emit('end')});
  });
  
  req.on('error', function(e) {
      console.error('Request Failure: ' + e.message);
   });
  
  req.write(data);
  req.end();
  };
  </code></pre>