title: Post and decode JSON In Node.js
body: <p>I am trying to post an associative array to another Node.js server, internaly to my server:</p>
  <p>First server does a <code>POST</code> request like this with <a href="https://www.npmjs.com/package/request" rel="nofollow noreferrer">Request</a>:</p>
  <pre><code>var request = require('request');
  request.post('http://localhost:8084/',{ json: {&quot;isposted&quot;: {&quot;ok&quot;:&quot;val&quot;}}});
  </code></pre>
  <p>Second server's result is this:</p>
  <pre><code>{ '{&quot;isposted&quot;:{&quot;ok&quot;:&quot;val&quot;}}': '' }
  </code></pre>
  <p>Instead of:</p>
  <pre><code>{&quot;isposted&quot;:{&quot;ok&quot;:&quot;val&quot;}}
  </code></pre>
  <p>Server's (2nd) source code to parse the data is this:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var http = require('http');
  var qs = require('qs');
  
  var processRequest = function(req,callback) {
      var body='';
      req.on('data', function(data) { body+=data; });
      req.on('end',  function() { callback(qs.parse(body)); });
  }
  
  var server2 = http.createServer(function(req, res) {
      processRequest(req,function(data){
          try
          {
              data=JSON.parse(data.jsonData);
          }
          catch(e)
          {
              data=data;
          }
          console.log(data);
      });
   });</code></pre>
  </div>
  </div>
  </p>