Express + Postman, req.body is empty

I know this has been asked several times, but I searched around and still cannot find the answer to my problem.

Here is my code, I have to use and configure the parser to determine the routes. I only use .json () with bodyParser, because now I am only testing the POST function, but I even tried to use 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"); }); 

This is how I use Postman to check this route. enter image description here

and here is the answer that I get

 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.

+11
source share
4 answers

AFAIK you need to use 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 to set the body as raw json:

 { "test": "yay" } 
+11
source

After spending several hours, I realized that I needed to change the type of Raw in the postman to JSON enter image description here

+35
source

In my case, I solve this by adding "type":"text" to the urlencoded element in the exported json collection file created by the postman. I observe this because some of my requests have been successfully fulfilled. The difference is that there is no type field in the postman collection file generated by json. This problem also happens to my teammate.

before (failed request): "body": { "mode": "urlencoded", "urlencoded": [ { "key": "email", "value": "{{userEmail}}" }, { "key": "password", "value": "{{userPassword}}" } ] }

after (successful request): "body": { "mode": "urlencoded", "urlencoded": [ { "key": "email", "value": "{{userEmail}}", "type": "text" }, { "key": "password", "value": "{{userPassword}}", "type": "text" } ] }

I also write a JavaScript parser script to handle this.

 const fs = require('fs'); let object = require(process.argv[2]); function parse(obj) { if(typeof obj === 'string') return; for(let key in obj) { if(obj.hasOwnProperty(key)) { if(key === 'urlencoded') { let body = obj[key]; for(let i = 0;i < body.length;i++) { body[i].type = "text"; } } else parse(obj[key]); } } } parse(object); fs.writeFile('ParsedCollection.json', JSON.stringify(object, null, '\t'), function(err){ //console.log(err); }); 

Just run it in the terminal node parser.js <json postman collection file path> and it will be the ParsedCollection.json file ParsedCollection.json . After that, import this file into the postman.

0
source

Try it

  // 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 })); 
0
source

Source: https://habr.com/ru/post/1235896/


All Articles