Can't get POST data using NodeJS/ExpressJS and Postman
Add the encoding of the request. Here is an example
..
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
..
Then select x-www-form-urlencoded
in Postman or set Content-Type to application/json
and select raw
Edit for use of raw
Raw
{
"foo": "bar"
}
Headers
Content-Type: application/json
EDIT #2 Answering questions from chat:
- why it can't work with form-data?
You sure can, just look at this answer How to handle FormData from express 4
- What is the difference between using
x-www-form-urlencoded
andraw
differences in application/json and application/x-www-form-urlencoded
let express = require('express');
let app = express();
// For POST-Support
let bodyParser = require('body-parser');
let multer = require('multer');
let upload = multer();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/api/sayHello', upload.array(), (request, response) => {
let a = request.body.a;
let b = request.body.b;
let c = parseInt(a) + parseInt(b);
response.send('Result : '+c);
console.log('Result : '+c);
});
app.listen(3000);
Sample JSON and result of the JSON:
Set Content-typeL application/JSON: