Supertest: post(url, data) does not send any data

Created on 22 Dec 2014  路  19Comments  路  Source: visionmedia/supertest

superagent allows the following syntax:

request(app).post('/url', { foo: 'bar' })

This should set the content-type to json and send the JSON data in the request body.

With supertest the above syntax results in no content-type and no body.

testcase:

  it('.post should work with data', function (done) {
    var app = express();

    app.use(express.bodyParser());

    app.post('/', function(req, res){
      res.send(req.body.name);
    });

    request(app)
    .post('/', { name: 'tobi' })
    .expect('tobi', done);
  })

Thanks.

Most helpful comment

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

All 19 comments

+1

use:

it('.post should work with data', function (done) {
    var app = express();

    app.use(express.bodyParser());

    app.post('/', function(req, res){
      res.send(req.body.name);
    });

    request(app)
    .post('/')
    .send({ name: 'tobi' })
    .expect('tobi', done);
  })

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

+1 Mikker.

Thanks a lot, mayby fix the Accept POST Header

         testServer
                    .post('/api/book/save/')
                    .type('form')
                    .send(data.book)
                    .set('Accept', /application\/json/)
                    .expect(201)
                    .end(function (err, res) { done(); });

Can we attach image file along with the same request without changing type('form'
) ?

This worked for me

import bodyParser from 'body-parser';
app.use(bodyParser.json());

+1 Montaro

+1 Montaro

Seems the readme has only this example, which can be very confusing.
Related: #168

request(app)
  .post('/')
  .field('name', 'my awesome avatar')
  .attach('avatar', 'test/fixtures/homeboy.jpg')

I didn't know there is another method:

request(app)
  .post('/api/book/save/')
  .type('form')
  .send(data.book)
  .set('Accept', /application\/json/)
  .expect(201)
  .end(function (err, res) { done(); });

It's weird that nested form data breaks in .field()
.field('name[en]', 'value') does not work, but .send({ name: { en: 'value' } }) works for me.
Don't know why.

+1

@fritx @zanemcca I don`t know how relevant is to see the help from this library for the supertest. But you can try using the documentation from it.
http://unirest.io/nodejs.html

Adding .type('form') fixed my problem too

adding these two lines solves this problem
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

Be sure you're not looking for .query(postData) instead of .send(postData)

it should use form as the default type instead of json.
where forms are standard http requests while jsons are file format.

I'm having trouble with a basic post with json body not coming through in Express 4.16.3 which has the body parsing stuff for json built in.

@samueljseay as a heads up expressjs version 4 and above does not include body parsing middleware. See the migration guide. You must use a separate middleware library like body-parser.

This worked for me

import bodyParser from 'body-parser';
app.use(bodyParser.json());

Nowadays, you should use express.json(), since bodyParser is currently deprecated.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hacker0limbo picture hacker0limbo  路  3Comments

Shingaz picture Shingaz  路  5Comments

schm1ty1 picture schm1ty1  路  4Comments

ryami333 picture ryami333  路  4Comments

mayacr86 picture mayacr86  路  4Comments