1

I have the following code in my routes file:

router.post('/submit', function(req, res) {
    var email = req.body.email;
    console.log(email);
});

I am making a post call from postman with following details: http://localhost:3000/login/submit

params:

email=abcxyz@gmail.com 

and headers i have tried both

Content-Type:application/json 

and

Content-Type: application/x-www-form-urlencoded

Also, I have body parser separately installed with following in app.js

var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

But, The console log for email shows 'undefined'. Why am i not able to grab the post params with req.body.email.

Ivan Vasiljevic
  • 5,478
  • 2
  • 30
  • 35
clint
  • 1,786
  • 4
  • 34
  • 60

3 Answers3

5

In my case

http://127.0.0.1:3000/submit

router.post('/submit', function(req, res) {
console.log("Your Email is "+req.body.email);
res.end(); // if you not end the response it will hanging...

enter image description here

set your header type only

Content-Type application/x-www-form-urlencoded

enter image description here

Body

enter image description here Console output enter image description here

Adiii
  • 54,482
  • 7
  • 145
  • 148
2

I faced the similar issue and got it working, please follow the below steps

  1. In Headers tab put content type as below

Content-Type: application/x-www-form-urlencoded

  1. In Body tab select x-www-form-urlencoded checkbox and put params.

email=abcxyz@gmail.com

Now in your node app use below code

router.post('/submit', function(req, res) {
    var email = req.body.email;
    console.log(email);
});
Suneel Kumar
  • 5,621
  • 3
  • 31
  • 44
1
    [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);

Please see the below example in the link
https://stackoverflow.com/questions/41955103/cant-get-post-data-using-nodejs-expressjs-and-postman/53514520#53514520

This will help you][1]

Set Body

Set Content-type

Goutam Ghosh
  • 87
  • 1
  • 7