req.body empty and or undefined
See original GitHub issueHello guys,
I am currently working on an authentication api using express and body-parser. But I ran into problems with the parser.
For example, here is my app.js:
var morgan = require('morgan');
var bodyParser = require('body-parser');
var passport = require('passport');
var express = require('express');
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json({ type: 'application/*+json' }));
app.use(morgan('dev'));
app.use(passport.initialize());
require('./config/passport')(passport);
var routes = require('./routes/index');
var register = require('./routes/register');
var authenticate = require('./routes/authenticate');
var dashboard = require('./routes/dashboard');
app.use('/', routes);
app.use('/register', register);
app.use('/authenticate', authenticate);
app.use('/dashboard', dashboard);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
Now let’s have a look at the register.js, located in routes/register.js:
var express = require('express');
var router = express.Router();
router.post('/', function (req, res, next) {
if (!req.body.email || !req.body.password) {
console.log(req.body);
console.log(req.body.email);
res.json({success: false, message: 'Please enter email and password.'});
} else {
var newUser = new User({
email: req.body.email,
password: req.body.password
});
// Attempt to save the user
newUser.save(function (err) {
if (err) {
return res.json({success: false, message: 'That email address already exists.'});
}
res.json({success: true, message: 'Successfully created new user.'});
});
}
});
module.exports = router;
I am currently sending JSON by using HttpRequester or RESTClient. No matter which one. Both will bring the same bug? I have been printing out “req.body” and “req.body.email” for example.
Case 1:
POST http://localhost:3000/register Content-Type: application/json { "email":"testmail@test.com", "password":"123456" }
Outputs:
{} undefined POST /register 200 0.350 ms - 62
Case 2
Same with url-encoded
POST http://localhost:3000/register Content-Type: application/x-www-form-urlencoded { "email":"testmail@test.com", "password":"123456" }
Outputs:
{ '{\n"email":"testmail@test.com",\n"password":"123456"\n}': '' } undefined POST /register 200 1.756 ms - 62
But why undefined?
Case 3
Working like normal post parameters
POST http://localhost:3000/register Content-Type: application/x-www-form-urlencoded email=testmail@test.com
Outputs:
{ email: 'testmail@test.com' } testmail@test.com POST /register 200 1.502 ms - 62
I personally don’t know why it is not parsing the json object. Maybe you know an answer?
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (4 by maintainers)
Top GitHub Comments
I think the issue is from your
type: 'application/*+json'
above. What are you trying to achieve with that?Hello and thank you for your quick answer.
I used
type: 'application/*+json'
for testing purposes. By the way, it seems that justbodyParser.json()
ignores the JSON sent because with the emptybodyParser.json()
I am getting an “no view engine error” , so I tried out the different settings which are documented like the setting of the type.That means: Using:
brings:
application/x-www-form-urlencoded
filled body - undefined body.emailapplication/json
empty body - undefined body.emailtext/plain
empty body - undefined body.emailtext/html
empty body - undefined body.emailUsing:
brings:
application/x-www-form-urlencoded
filled body - undefined body.emailapplication/json
empty body - “No default engine” errortext/plain
empty body - undefined body.emailtext/html
empty body - undefined body.emailIt also seems that there is no difference between:
app.use(bodyParser.urlencoded({ extended: false }));
andapp.use(bodyParser.urlencoded({ extended: true}));
in my case. Or swapping urlencoded with json.UPDATE: I found out that if I
use x-www-form-urlencoded
the body-parser tries to doubleparse that object:{ '{\n"email":"test@test.com",\n"password":"123456",\n"test":"123456"\n}': '' }
Here is a small workaround for this:But this is not really what was intended because I want to use the bodyParser.json() and not an
use x-www-form-urlencoded
workaround to work with json objects.