title: Node.js express.json middleware not parsing request as expected
body: <p>I have a simple cURL (i think this is right) that posts a small JSON object to my express server:</p>
  
  <pre><code>curl -d "{'test': 'this'}" localhost:3000/rest/user/authenticate
  </code></pre>
  
  <p>I have express set up as:</p>
  
  <pre><code>// set up body parsing in express  to be able  to get parse JSON posts
  server.use(express.json());
  server.use(express.urlencoded());
  </code></pre>
  
  <p>and have handler that accepts the route:</p>
  
  <pre><code>JSON = require('JSON')
  module.exports = {
      authenticateUser: function create(req, res){
          var postedObject = req.body
          console.log(postedObject)
          res.send('Handle Post: authenticateUser');
      }
  }
  </code></pre>
  
  <p>the handler is getting called, but it is logging the JSON body unexpectedly:</p>
  
  <pre><code>{ '{\'test\': \'this\'}': '' }
  </code></pre>
  
  <p>So my entire object looks to be the name side of a JSON Name:Value pair object. no matter what I post it seems to be appending the value side.  Unless  I do something like this:</p>
  
  <pre><code>curl -d "a=a" localhost:3000/rest/user/authenticate
  </code></pre>
  
  <p>which logs:</p>
  
  <pre><code>{'a':'a'}
  </code></pre>
  
  <p>so have i not set the right headers? Configured express wrong?  I plan on digging through the express code, but wondered if somebody might know before I find the solution.  Either way having a searchable/indexed answer to this on the web will be nice.</p>
  
  <h2>update 1</h2>
  
  <p>ok I need to add the header to the cURL</p>
  
  <pre><code>curl -H "Content-Type: application/json" -d "{'test': 'this'}" localhost:3000/rest/user/authenticate
  </code></pre>
  
  <p>which gives  the error:</p>
  
  <pre><code>Parsing: {'test': 'this'}
  SyntaxError: Unexpected token '
      at Object.parse (native)
      at C:\blah\node_modules\express\node_modules\connect\lib\middleware\json.js:86:19
          at IncomingMessage.onEnd (C:blah\node_modules\express\node_modules\connect\node_modules\raw-body\index.js:109:7)
          at IncomingMessage.g (events.js:180:16)
          at IncomingMessage.EventEmitter.emit (events.js:92:17)
          at _stream_readable.js:920:16
          at process._tickCallback (node.js:415:13)
  </code></pre>
  
  <p>OR 
      curl -H "Content-Type: application/json" -d "{test: 'this'}" localhost:3000/rest/user/authenticate</p>
  
  <p>which gives  the error:</p>
  
  <pre><code>Parsing: {test: 'this'}
  SyntaxError: Unexpected token t
      at Object.parse (native)
      at C:\blah\node_modules\express\node_modules\connect\lib\middleware\json.js:86:19
          at IncomingMessage.onEnd (C:blah\node_modules\express\node_modules\connect\node_modules\raw-body\index.js:109:7)
          at IncomingMessage.g (events.js:180:16)
          at IncomingMessage.EventEmitter.emit (events.js:92:17)
          at _stream_readable.js:920:16
          at process._tickCallback (node.js:415:13)
  </code></pre>
  
  <h2>update 2</h2>
  
  <p>in the file connect/lib/middleware/json.js</p>
  
  <p>this line seems to be the one causing issues</p>
  
  <pre><code>req.body = JSON.parse(buf, options.reviver);
  </code></pre>
  
  <h2>update 3</h2>
  
  <p>I really think it is my cURL  </p>
  
  <pre><code>buf= JSON.stringify({test: 'This'});
  console.log(buf)
  req.body = JSON.parse(buf, options.reviver);
  </code></pre>
  
  <p>works logging first 
      {"test":"this"}</p>
  
  <p>and then in my handler:</p>
  
  <pre><code>----------------------
  { test: 'this' }
  ----------------------
  </code></pre>