title: JSON error in node js undefined as prefix to post data
body: <p>I'm having trouble posting json to a little node.js http server. Post data always seems to have an 'undefined' on the front of it. I'm probably doing really stupid, so my apologies!</p>
  
  <p>I start the server and post some json with the below py script:</p>
  
  <pre><code>&gt;&gt;node simplehttp.js
  &gt;&gt;python post.py '{"foo":"bar"}'
  </code></pre>
  
  <p>The server gets this</p>
  
  <pre><code>&gt;&gt;Request received: undefined{"foo": "bar"}
  Invalid JSON:undefined{"foo": "bar"}
  </code></pre>
  
  <p>node http server</p>
  
  <pre><code>var http = require("http"); // http-server
  
  var server_http = http.createServer(
      // Function to handle http:post requests, need two parts to it
      // http://jnjnjn.com/113/node-js-for-noobs-grabbing-post-content/
      function onRequest(request, response) {
          request.setEncoding("utf8");
  
          request.addListener("data", function(chunk) {
              request.content += chunk;
          });
  
          request.addListener("end", function() {
              console.log("Request received: "+request.content);
  
              response.writeHead(200, {"Content-Type": "text/plain"});
              response.write("Thanks for sending a message");
              response.end();
  
              try {
                  json = JSON.parse(request.content);
                  if(json.m !== undefined){
                      console.log("m: "+json.m);
                  }
  
              } catch (Error) {
                  console.log('Invalid JSON:' + request.content);
              }
          });
      }
  );
  
  server_http.listen(9002);
  </code></pre>
  
  <p>python script to do post</p>
  
  <pre><code>import sys
  import json
  import httplib, urllib, urllib2
  
  # Get parameters
  if len(sys.argv) &lt; 2:
      sys.stderr.write('Usage: python post.py [JSON Message]\n')
      sys.exit(1)
  
  values = json.loads(sys.argv[1])
  headers = {"Content-type": "application/json"}
  
  conn = httplib.HTTPConnection('127.0.0.1', 9002)
  headers = {"Content-type": "application/json"}
  conn.request("POST", "", json.dumps(values), headers)
  response = conn.getresponse()
  
  print "response.status: "+response.status
  print "response.reason: "+response.reason
  print "response.read: "+response.read()
  conn.close()
  </code></pre>