admin管理员组文章数量:1307530
I've tried the all solutions from some another stackoverflow posts but it didn't solved my issue.
Here is my app.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// unment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
var index = require('./routes/index');
var v1 = require('./routes/route');
app.use('/', index);
//routes for api
app.use('/v1',v1);
Here is my post
controller
module.exports = {
createUser:function (req,res) {
console.log(req.body);
res.send('ok'+req.body.test);
}
}
req.body
returns {}
even if the request body contains the parameters.
I am checking the api's with postman plugin.
Update
Postman request
I've tried the all solutions from some another stackoverflow posts but it didn't solved my issue.
Here is my app.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// unment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
var index = require('./routes/index');
var v1 = require('./routes/route');
app.use('/', index);
//routes for api
app.use('/v1',v1);
Here is my post
controller
module.exports = {
createUser:function (req,res) {
console.log(req.body);
res.send('ok'+req.body.test);
}
}
req.body
returns {}
even if the request body contains the parameters.
I am checking the api's with postman plugin.
Update
Postman request
Share Improve this question edited Apr 5, 2017 at 10:00 Jabaa asked Apr 5, 2017 at 9:53 JabaaJabaa 1,7637 gold badges36 silver badges63 bronze badges 20- 1 You need to send json, not form-data – Creynders Commented Apr 5, 2017 at 10:03
- 1 Yes, there is, you need to select "raw" and select "application/json" – Creynders Commented Apr 5, 2017 at 10:09
-
1
in raw options did you select type
JSON(application/json)
– parwatcodes Commented Apr 5, 2017 at 10:15 -
1
Ah, it's not real JSON, it should be
{"test":"hello"}
– Creynders Commented Apr 5, 2017 at 10:21 -
1
application/json
is used when you are posting the data{"test":"hello"}
like this,www-form-url-encoded
is used to get the data as key-value in object from the url when used thebodyParser.urlencoded
, They both are different and have their own use cases – parwatcodes Commented Apr 5, 2017 at 10:30
2 Answers
Reset to default 5body-parser
The bodyParser object exposes various factories to create middlewares. All middlewares will populate the req.body
property with the parsed body, or an empty object {}
if there was no body to parse (or an error was returned).
app.use(bodyParser.urlencoded({ extended: true })); // for encoded bodies
A new body object containing the parsed data is populated on the request object after the middleware,
req.body
will contain the parsed data, this object will contain key-value pairs, where the value can be a string or array
The Content-Type is application/x-www-form-urlencoded
app.use(bodyParser.json()); // for json encoded bodies
A new body object containing the parsed data is populated on the request object after the middleware (i.e.
req.body
).
The Content-Type is application/json
application/json
is used when you are posting the data {"test":"hello"}
like this. www-form-url-encoded
is used to get the data as key-value in object from the url when used the app.use(bodyParser.urlencoded({ extended: true }));
. They both are different and have their own use cases
After removing the last 4 lines of code (to be sure you are configuring correctly the routes) and adding this test lines:
app.post('/ping', function (req,res) {
console.log(req.body);
res.send('ok ' + req.body.test);
});
let server = http.createServer(app);
server.listen(8899, function onstart() {
console.log('server listening');
});
When I run:
curl -X POST http://localhost:8899/ping -d '{"test": 1234}'
I get ok undefined
, like you did. After adding the proper content-type
header:
curl -X POST http://localhost:8899/ping -d '{"test": 1234}' -H "content-type: application/json"
it works like a charm and I get ok 1234
. So I think you are missing the "content-type: application/json"
header in your postman.
本文标签: javascriptExpress js reqbody returns emptyStack Overflow
版权声明:本文标题:javascript - Express js req.body returns empty - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741819573a2399284.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论