How to send a JSON in a POST request? I'm trying to set the 'content-type' headers but is not working for me.
Here is a quick example:
it('should POST JSON', function(done) {
supertest(app)
.post('/postjson')
.send({
'id': 1,
'name': 'Mike'
})
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.expect(200)
.end(function (err, res) {
if (err) throw err;
console.log(res.body);
done();
});
});
Oh I found the answer in another question but really thanks @mikelax
It would be useful to share a link to the question where you found your answer.
@sirgalleto reminds me of https://xkcd.com/979/ :)
i've got the same problem, my test request:
const body = {
type: 'high',
product: 300
};
const res = await req
.post('/apply')
.send(body)
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
solution:
Put body-parser always after express object and before every routes in main server file like this
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
https://stackoverflow.com/questions/39470566/request-body-undefined-in-supertest
Most helpful comment
Here is a quick example: