-
Notifications
You must be signed in to change notification settings - Fork 758
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
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
|
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 |
Adding
And not using |
Without seeing the server code it is impossible to know where the issue might be. |
So the code below works for me now:
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 |
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. 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 server.js const bodyParser = require('body-parser') // just config like this app.post('/calc/zakat', validation.isMustHaveBody); my testfile.js |
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 |
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 |
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.
The text was updated successfully, but these errors were encountered: