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

post(url, data) does not send any data #189

Closed
SystemParadox opened this issue Dec 22, 2014 · 20 comments
Closed

post(url, data) does not send any data #189

SystemParadox opened this issue Dec 22, 2014 · 20 comments

Comments

@SystemParadox
Copy link

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.

@JerryC8080
Copy link

+1

@gabeio
Copy link

gabeio commented Jan 19, 2015

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

@mikker
Copy link

mikker commented Feb 6, 2015

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

@joncodo
Copy link

joncodo commented Feb 19, 2015

+1 Mikker.

@fernandoPalaciosGit
Copy link

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

@muneermuhammed
Copy link

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

@montaro
Copy link

montaro commented Jan 27, 2016

This worked for me

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

@muneermuhammed
Copy link

+1 Montaro

1 similar comment
@snackycracky
Copy link

+1 Montaro

@fritx
Copy link

fritx commented May 24, 2016

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.

@zanemcca
Copy link

+1

@akanshgulati
Copy link

@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

@WwupengP
Copy link

WwupengP commented Jul 7, 2017

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

@omar-meshaal
Copy link

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

@NoBrainSkull
Copy link

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

@calidion
Copy link

calidion commented Dec 5, 2017

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

@samueljseay
Copy link

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.

@mikelax
Copy link
Contributor

mikelax commented May 17, 2018

@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.

@mikelax mikelax closed this as completed May 17, 2018
@olegario96
Copy link

olegario96 commented Mar 21, 2021

This worked for me

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

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

@gsanikidze
Copy link

gsanikidze commented Apr 26, 2021

I want send multipart/form-data and req.body is always empty object.

`test('multipart/form-data', async (done) => {
const app = express();

app.use(express.json());

app.post('/', (req, res) => {
console.log(req.body); // logs empty object -> {}

done();
return res.send();

});

await request(app)
.post('/')
.field('some', 'field')
.attach('some_file', fs.readFileSync(${__dirname}/index.ts), { filename: 'some_file_name' });
});`

supertest=6.1.3
express=4.17.1
node=14.16.1

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