Supertest: No body being sent on post.

Created on 4 Oct 2014  路  17Comments  路  Source: visionmedia/supertest

supertest(test_app)
.post('/column')
.send({
locale: 'en',
type: 'ap_list',
columns: ['name','ap_folder_id','antenna_type_1']
})
.expect(200)
.expect('Content-Type', /html/)
.end(function(err, res) { /* More tests */ })

The callback for the '/column' route receives nothing in req.body. In fact, the 'body' key does not appear in req at all. This is similar to #93 , but not the same. This is specific to a post. I tried removing the two 'expects', but this had no effect on the operation of the test.

Most helpful comment

Ahh, turns out this is not a problem with supertest. If you're using express, you'll need to manually include the body-parser module.

var bodyParser = require('body-parser')
var express = require('express')

var app = express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))

All 17 comments

I am having a similar issue.

Same here ...

Also seeing this

Ahh, turns out this is not a problem with supertest. If you're using express, you'll need to manually include the body-parser module.

var bodyParser = require('body-parser')
var express = require('express')

var app = express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))

mmm I am including body-parser. In non testing mode the application works fine. In testing mode it seems that the body isn't sent.

Maybe try adding .set('Accept', /json/) before the send funcition

Adding .type('form') fixed this for me.

Adding .type('form') just changed how my data was sent (which confused and infuriated my server). I've managed to get a happy JSON structure out by adding a callback and done() to my last except:

.expect('Content-Type', /json/)  
.expect(200,function(err,result){
    assert.equal(result.body.status,"success");
    assert(result.body.hasOwnProperty('cohort'));
    done();
});

And not using .end at all.

Without seeing the server code it is impossible to know where the issue might be.

So the code below works for me now:

.end(function(err,result) {
            console.log(err);
            console.log(JSON.stringify(result));
            assert.equal(result.body.status,"success");
            assert(result.body.hasOwnProperty('cohort'));
            done();
        });

The weird thing is that the log doesn't show result having a "body" field so I didn't try to access it. Seems I've got some learning to do because I haven't seen that before.

I have the same problem - body is not sent with post

I forgot body-parser too, it works for me now, thanks

+1

@zanemcca this is working correctly as it should. Here is a snippet to start with.
Notice I am not even setting the content-type but you probably should do that as well.

var express = require('express');
var supertest = require('supertest');
var compression = require('compression');
var bodyParser = require('body-parser');
var path = require('path');

var app = express();
app.use(compression());
app.use(bodyParser.json());

app.post('/postjson', function(req, res) {
  console.log('In the post function');
  res.json(req.body).status(200);
});

describe('Test POST with supertest', function() {
  it.only('should POST JSON', function(done) {
    supertest(app)
      .post('/postjson')
      .send({
        'id': 1,
        'name': 'Mike'
      })
      .set('Accept', 'application/json')
      .expect(200)
      .end(function (err, res) {
        if (err) throw err;
        console.log(res.body);
        done();
      });
  });
});

its working, if you previously add body-parser in your app, and you just set type form and add set Accept json

server.js
`

const bodyParser = require('body-parser')
const app = express();

// just config like this
yourApp.use(bodyParser.json())
yourApp.use(bodyParser.urlencoded({ extended: true}))

app.post('/calc/zakat', validation.isMustHaveBody);
`

my testfile.js
it('POST => /calc/zakat should be error', (done) => { server .post('/calc/zakat') .type('form') //set this .set('Accept', /json/) set this too .send({ monthly_incomes : 0, other_incomes: 0, debts: 0 }) // and now you can send your data .expect(400) .expect('Content-type', /json/) .end((err, res) => { res.status.should.equal(400) res.body.error.should.equal(true) done() }) })

Error: expected 200 "OK", got 415 "Unsupported Media Type"

@ithillel-aminev - if you are using jest it might be related to https://stackoverflow.com/questions/49141927/express-body-parser-utf-8-error-in-test?rq=1

Was this page helpful?
0 / 5 - 0 ratings

Related issues

NBNARADHYA picture NBNARADHYA  路  4Comments

nitrocode picture nitrocode  路  4Comments

nazreen picture nazreen  路  3Comments

DeaconDesperado picture DeaconDesperado  路  6Comments

jon301 picture jon301  路  5Comments