1

I'm having a lot of trouble sending a request body in a supertest post. I've read the solutions to other questions, which blame improper configuration of body-parser, but those answers are referring to a custom datatype. (res.body is empty in this test that uses supertest and Node.js). I configure body-parser like this:

var bodyParser = require('body-parser');
app.use(bodyParser.json({ limit: '50mb', type: 'application/vnd.api+json' }));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));

This solution to the post I've provided says "supertest uses this file to determine if you are sending json or not." My content type is actually listed there, so I don't see why this should be a problem.

I am attempting to post a request like so:

it ('creates user', function (done) {
    var user = {
      firstname: 'Joe',
      lastname: 'JoeJoe',
      email: 'joe@joe.com',
      password: 'spartacus',
    };

    request(app)
      .post('/api/users')
      .send(user)
      .expect(200)
      .expect('Sent email to joe@joe.com', done)
  });
});

And I import my app with this line:

var app = require('../server.js');

Where server.js configures my application entirely. Is this application type supported by supertest? Is there a way to force the user data to be noticed as part of req.body?

Community
  • 1
  • 1
ritmatter
  • 3,448
  • 4
  • 24
  • 43

1 Answers1

0

I believe the issue can be solved by setting the content-type header on your post request to application/vnd.api+json. You can do so in supertest/superagent as follows:

it ('creates user', function (done) {
    var user = {
      firstname: 'Joe',
      lastname: 'JoeJoe',
      email: 'joe@joe.com',
      password: 'spartacus',
    };

    request(app)
      .post('/api/users')
      .set('Content-Type', 'application/vnd.api+json')
      .send(user)
      .expect(200)
      .expect('Sent email to joe@joe.com', done)
  });
});
JME
  • 3,592
  • 17
  • 24