title: How to mock POST request using mock-http-server npm module in nodejs?
body: <p>I am using 'chai-http' to test rest APIs and 'mock-http-server' to mock http requests. I am able to implement mock GET request by doing the following -</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>it ('API GET TEST', function (done) {
  
          mockserver.on({
              method: 'GET',
              path: '/v1/myAPI',
              reply: {
                       status: 200,
                       headers: {"content-type": "application/json"},
                       body: JSON.stringify(response)
                     }
              });
  
             chai.request(myapp)
              .get('/v1/myAPI')
              .end(function(err, res) {
                  res.should.have.status(200);
                  res.should.be.json;
                  done();
              })
      });</code></pre>
  </div>
  </div>
  </p>
  
  <p>My chai request correctly gets the response I have send from mockserver GET /v1/myAPI. </p>
  
  <p>What I want to do is mock a post request and depending on post body I want to send response. </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>        mockserver.on({
              method : 'POST',
              path : '/v1/myPOSTAPI',
              reply: {
                  status:  200,
                  headers: { "content-type": "application/json" },
                  body: function(req) {
  
                      if (req.body.id == 1) {
                          JSON.stringify(response1);
                      } else {
                          JSON.stringify({"error" : "Not Found"});
                      }
                  }
              }
  
  
          });</code></pre>
  </div>
  </div>
  </p>
  
  <p>My POST body is - </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>{
    id : 1
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>But when I use mock API for post, my 'req' object does not contain post body. How can I get post body by mocking post request using 'mock-http-server' ?</p>