Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express + Postman, req.body is empty

Tags:

I know this has been asked multiple times, but I have been looking around and still can't find an answer to my problem.

Here is my code, I make sure to use and configure body parser before defining the routes. I'm only using .json() with bodyParser because right now I'm only testing a POST function, but I've even tried with app.use(bodyParser.urlencoded({ extended: true }));

var express = require('express'),     bodyParser = require('body-parser'),     app = express();  app.use(bodyParser.json()); app.set('port', (process.env.PORT || 5000));  app.listen(app.get('port'), function() {     console.log("Node app is running at localhost:" + app.get('port')) });  app.post('/itemSearch', function(req, res) {     //var Keywords = req.body.Keywords;     console.log("Yoooooo");     console.log(req.headers);     console.log(req.body);     res.status(200).send("yay"); }); 

Here is how I use Postman to test this route. enter image description here

and here is the response I receive

Node app is running at localhost:5000 Yoooooo { host: 'localhost:5000',   connection: 'keep-alive',   'content-length': '146',   'cache-control': 'no-cache',   origin: 'chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop',   'content-type': 'multipart/form-data; boundary=----WebKitFormBoundarynJtRFnukjOQDaHgU',   'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36',   'postman-token': '984b101b-7780-5d6e-5a24-ad2c89b492fc',   accept: '*/*',   'accept-encoding': 'gzip, deflate',   'accept-language': 'en-GB,en-US;q=0.8,en;q=0.6' } {} 

At this point I would really appreciate any help. Thanks.

like image 821
seongju Avatar asked Nov 13 '15 15:11

seongju


People also ask

How do I check if a REQ is empty in node?

body is empty, it returns an empty object, as such, making ! req. body return false even when it's empty. Instead, you should test for !

What is req body in Express?

The req. body object allows you to access data in a string or JSON object from the client side. You generally use the req. body object to receive data through POST and PUT requests in the Express server.

What is inside req body?

The req. body property contains key-value pairs of data submitted in the request body. By default, it is undefined and is populated when you use a middleware called body-parsing such as express. urlencoded() or express. json().


2 Answers

After spending a few hours I realized need to change the Raw type in postman to JSONenter image description here

like image 86
David A Avatar answered Sep 23 '22 13:09

David A


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" } 
like image 28
nakwa Avatar answered Sep 23 '22 13:09

nakwa