Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No body being sent on post. #168

Closed
aikimcr opened this issue Oct 3, 2014 · 18 comments
Closed

No body being sent on post. #168

aikimcr opened this issue Oct 3, 2014 · 18 comments

Comments

@aikimcr
Copy link

aikimcr commented Oct 3, 2014

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.

@Ouwen
Copy link

Ouwen commented Oct 13, 2014

I am having a similar issue.

@stefanwalther
Copy link

Same here ...

@Emerson
Copy link

Emerson commented Oct 23, 2014

Also seeing this

@Emerson
Copy link

Emerson commented Oct 23, 2014

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 }))

@Ouwen
Copy link

Ouwen commented Oct 24, 2014

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.

@janajri
Copy link

janajri commented Dec 10, 2014

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

@mikker
Copy link

mikker commented Feb 6, 2015

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

@EnFinlay
Copy link

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.

@defunctzombie
Copy link

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

@EnFinlay
Copy link

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.

@royts
Copy link

royts commented Mar 21, 2016

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

@benleung
Copy link

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

@zanemcca
Copy link

+1

@mikelax
Copy link
Contributor

mikelax commented Jul 5, 2016

@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();
      });
  });
});

@ridhoassuryadi
Copy link

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() }) })

@tim8917
Copy link

tim8917 commented May 30, 2018

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

@joshuavial
Copy link

@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

@felipeLeao18
Copy link

felipeLeao18 commented Feb 16, 2023

I was building the routes before passing the express.json() when building my app instance, so change the order to use express.json() before setting up the routers worked for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests