user3630294
user3630294

Reputation: 51

ember http-mock request.body is undefined

just migrated to ember-cli 0.1.12 and now http-mock is not working for me.

Its working fine for get request . request.query returns all the query parameters.

However for POST request i cannot get the request paramters as request.body is undefined.

Can some one let me know how to access the request body in ember http-mock?

Upvotes: 1

Views: 637

Answers (2)

Adam Knights
Adam Knights

Reputation: 2151

I had the same issue with req.body being undefined. Here are the steps I followed:

1) Install body-parser

npm install body-parser

2) You need to specify for each mock file that it requires the body-parser. So for example my /server/mocks/addresses.js is now:

module.exports = function(app) {
  var express = require('express');
  var addressesRouter = express.Router();

  ...

  addressesRouter.post('/', function (req, res) {
    var address = req.body;
    address.id = addresses.length + 1;
    addresses.push(address);
    res.status(201).send({
      'address': address
    });
  });

  ...

    app.use('/api/addresses', require('body-parser').json(), addressesRouter);

So basically you need to add a second (middle) argument of require('body-parser').json() to the final app.use.

req.body should now work and no longer be undefined.

Upvotes: 1

joostdevries
joostdevries

Reputation: 930

As I've also said in the issue tracker, this is due to a change in Ember CLI where the body-parser was removed by default because it would affect other middlewares.

I've opened a PR with some instructions for your case. https://github.com/ember-cli/ember-cli/pull/3211

Upvotes: 1

Related Questions