Andre
Andre

Reputation: 3

How do I get my example bodyparser working?

I'm trying to send a post method with postman to receive some example parameters. However my example code won't recognize the req.body and so it is always empty.

This is my example code:

const express = require('express');
const bodyParser = require('body-parser');
const app = express();


app.use(bodyParser())
app.use(bodyParser.urlencoded({ extended: true }));

app.post('/', function(req, res){
  console.log(req.body)
})

app.listen(2001, function () {
  console.log('Listening on port 2001!');
});

If i use postman like this: Click here!

I always get an empty console.log

Upvotes: 0

Views: 29

Answers (1)

Marcos Casagrande
Marcos Casagrande

Reputation: 40404

You're sending the arguments as query string, so instead of using: req.body you have to use req.query

bodyParser will parse x-www-form-urlencoded in your case, since you're using:

app.use(bodyParser.urlencoded({ extended: true }));

Upvotes: 1

Related Questions