title: Node js + Express 4 parsing JSON POST request
body: <p>Having a problem with something that should be relatively simple. I am sending a POST request with JSON DATA as follows :</p>
  
  <pre><code>curl -H "Content-Type : application/json" -X POST -d '{"searchbar":"cat"}' http://someurl.com/public/search
  </code></pre>
  
  <p>Within my application I have a route ... under '/routes'
  I have set the bodyParser as follows:</p>
  
  <pre><code>router.use(bodyParser.json());
  router.use(bodyParser.urlencoded({ extended: true }));
  </code></pre>
  
  <p>In the route I have 
      var searchbar = req.body.searchbar</p>
  
  <p>However the variable searchbar is always undefined.</p>
  
  <p>I can see the data in the body as follows:</p>
  
  <pre><code>{ '{searchbar:cat}': '' }
  </code></pre>
  
  <p>Obviously the data is either not being sent correctly by the curl (so my curl is incorrect) or the data is not being parsed by the bodyParser. Can anyone help ?</p>
  
  <p><strong>EDIT : routes/index.js</strong></p>
  
  <pre><code>var express = require('express');
  var bodyParser = require('body-parser');
  var moment = require('moment');
  var fs = require('fs');
  var router = express.Router();
  
  router.use(bodyParser.json());
  router.use(bodyParser.urlencoded({ extended: true }));
  
  router.all('*',function(req,res,next) {
      res.header("Access-Control-Allow-Origin", "*");
      res.header("Access-Control-Allow-Headers", "Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With");
      res.header("Access-Control-Allow-Methods", "GET, PUT, POST, OPTIONS");
      next();
  
  });
  
  
  router.post('/public/search/',  function (req, res) {
      res.header("Access-Control-Allow-Origin", "*");
      res.header("Access-Control-Allow-Headers", "Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With");
      res.header("Access-Control-Allow-Methods", "GET, PUT, POST, OPTIONS");
  
      var searchbar = req.body.searchbar;
      console.log("Searchbar is : " + searchbar);
      console.log("body is : " + require('util').inspect(req.body, false, null)); 
      });
  
  modeul.exports = router;
  </code></pre>
  
  <p><strong>UPDATE:</strong> </p>
  
  <p>I used postman (chrome plugin) and managed to send and parse the data - there must be an issue with CURL in this instance ...</p>