Express + Postman, req.body is empty
Try this
// create application/json parser
app.use(bodyParser.json());
// parse various different custom JSON types as JSON
app.use(bodyParser.json({ type: 'application/*+json' }));
// parse some custom thing into a Buffer
app.use(bodyParser.raw({ type: 'application/vnd.custom-type' }));
// parse an HTML body into a string
app.use(bodyParser.text({ type: 'text/html' }));
// parse an text body into a string
app.use(bodyParser.text({ type: 'text/plain' }));
// create application/x-www-form-urlencoded parser
app.use(bodyParser.urlencoded({ extended: false }));
AFAIK you need to use the Body-Parser : https://github.com/expressjs/body-parser
bodyParser = require('body-parser').json();
app.post('/itemSearch', bodyParser, function(req, res) {
//var Keywords = req.body.Keywords;
console.log("Yoooooo");
console.log(req.headers);
console.log(req.body);
res.status(200).send("yay");
});
Then try with PostMan setting the body as raw
json:
{
"test": "yay"
}
After spending a few hours I realized need to change the Raw type in postman to JSON
I wanted to add an answer, as was seeming to have trouble getting sending as form-data
to work, even if I was adding Content-Type: multipart/form-data
to the Header (this was listed as correct header type in docs). I wonder if because using BodyParser in express, data has to come in as JSON raw. I swear I got form-data
to work before, alas.
Here's how I was able to get req.body
not to be empty:
- Make sure in "Headers" tab, you have this key value pair setup:
Content-Type: application/json
Side note: interesting link to stack overflow article on all available header content type values.
- In "Body" tab, make sure
raw
radio button is selected, and far right dropdown hasJSON
selected:
- Now if I console log
req.body
in my express app, I see printed: