body-parser

Can not post the nested object json to node express body parser

南笙酒味 提交于 2019-12-04 14:51:32
Hi I'm creating sample REST api using Node, Express and Mongo. I'm using bodyParser() middle ware to parse the form data. Its working fine for simple object say var user = { name:'test', age:'20' } req.body produce the same set of format to save it in the mongodb like. { name:'test', age:'20' } When using complex object var user = { name:'test', age:'20', education: { institute:"xxx", year:2010 } } req.body produce different format something like { name:'test', age:'20', education[institute]: "xxx", edcuation[year]:2010 } I would like to get the same format which i post in the body to save

Error: invalid json with multer and body-parser

天大地大妈咪最大 提交于 2019-12-03 12:25:21
问题 I am currently using multer for multipart/form-data in node.js application alongside with body-parser. I tried to POST form-data using POSTMAN, but it is getting this error. Error: invalid json at parse (/Users/k/Documents/application/node_modules/body-parser/lib/types/json.js:79:15) at /Users/k/Documents/application/node_modules/body-parser/lib/read.js:102:18 at IncomingMessage.onEnd (/Users/k/Documents/application/node_modules/body-parser/node_modules/raw-body/index.js:136:7) at

Error: invalid json with multer and body-parser

落花浮王杯 提交于 2019-12-03 03:35:19
I am currently using multer for multipart/form-data in node.js application alongside with body-parser . I tried to POST form-data using POSTMAN, but it is getting this error. Error: invalid json at parse (/Users/k/Documents/application/node_modules/body-parser/lib/types/json.js:79:15) at /Users/k/Documents/application/node_modules/body-parser/lib/read.js:102:18 at IncomingMessage.onEnd (/Users/k/Documents/application/node_modules/body-parser/node_modules/raw-body/index.js:136:7) at IncomingMessage.g (events.js:199:16) at IncomingMessage.emit (events.js:104:17) at _stream_readable.js:908:16 at

bodyParser doesn't seems to work

大城市里の小女人 提交于 2019-12-02 10:04:57
问题 Sorry if this question may sounds "simple", but I can't get body-parser to work on this very simple example : "use strict"; const PORT = 3000; const express = require("express"); const bodyParser = require("body-parser"); const app = express(); app.post("/api/login", (req, res) => { if (!req.body) return res.sendStatus(400); res.send("welcome, " + req.body.username); }); app.use(express.json()); app.use(bodyParser.json()); console.log(`Listen on port ${PORT}`); app.listen(PORT); Command line

Express body-parser handling checkbox arrays on forms

社会主义新天地 提交于 2019-12-01 21:41:22
I have an HTML form with an array of checkboxes (using [] naming). I need to be able to process this with express . I'm using body-parser . The problem is that unchecked checkboxes don't submit a value, and at the same time, body-parser seems to remove "holes" in arrays by simply packing the values into an array in order of indices, but ignoring the indices themselves. (Update: Actually it looks like qs is the culprit ). Consider this full example, which displays a form and responds with a JSON dump of the submitted data: Install: npm install express body-parser index.js: var express = require

Express Parsing Array from POST

人盡茶涼 提交于 2019-12-01 19:55:57
问题 Running an Express API, I'm struggling to parse data including an array of objects correctly when hitting a POST route. A simplified version of the code - var express = require('express'); var app = express(); var router = express.Router(); var bodyParser = require('body-parser'); var parseUrlencoded = bodyParser.urlencoded({ extended: false }); router.post('/', parseUrlencoded, function(req, res) { console.log(req.body); }); Hitting the API with the following { name: "Object name",

body is empty when parsing DELETE request with express and body-parser

白昼怎懂夜的黑 提交于 2019-12-01 18:56:17
I'm using expressjs and the body-parser middleware. This is how I initiate it: app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); From the client I'm sending a DELETE request and when I try to pick it up from the server side I get an empty object: app.delete('/', function(req, res) { console.log(util.inspect(req.body)); //outputs {} //some more code }); however when I send it with a POST I get what I need: app.post('/delete', function(req, res) { console.log(util.inspect(req.body)); //outputs { mid: 'ffw1aNh2' } //some more code }); It is worth noting that I don't

Express Parsing Array from POST

时光毁灭记忆、已成空白 提交于 2019-12-01 17:51:23
Running an Express API, I'm struggling to parse data including an array of objects correctly when hitting a POST route. A simplified version of the code - var express = require('express'); var app = express(); var router = express.Router(); var bodyParser = require('body-parser'); var parseUrlencoded = bodyParser.urlencoded({ extended: false }); router.post('/', parseUrlencoded, function(req, res) { console.log(req.body); }); Hitting the API with the following { name: "Object name", arrayOfObjects: [ { name: "Sub Object Name", subType: "Sub Object Type" }, { name: "Sub Object Name 2", subType:

The JSON data in request body is not getting parsed using body-parser

China☆狼群 提交于 2019-12-01 14:13:44
问题 When I send a POST request using postman to localhost:8080/api/newUser with request body: {name: "Harry Potter"} At server end console.log(req.body) prints: { '{name: "Harry Potter"}': '' } server.js var express = require('express'); var app = express(); var router = express.Router(); var bodyParser = require('body-parser'); app.use('/', express.static(__dirname)); router.use(function(req, res, next) { next(); }); router .route('/newUser') .post(function(req, res) { console.log(req.body); });

Writing express middleware to get raw request body before body-parser

孤街浪徒 提交于 2019-12-01 09:29:37
I wrote an Express middleware to retrieve the raw body from the request, and I set it before body-parser middleware. My custom middleware is calling req.setEncoding('utf8') , but this causes the following body-parser error: Error: stream encoding should not be set at readStream (/node_modules/body-parser/node_modules/raw-body/index.js:211:17) at getRawBody (/node_modules/body-parser/node_modules/raw-body/index.js:106:12) at read (/node_modules/body-parser/lib/read.js:76:3) at jsonParser (/node_modules/body-parser/lib/types/json.js:127:5) Here is my code: var express = require('express'); var