1
Good morning!
I created an HTTP server using nodejs as follows:
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
extended: true
}));
app.post('/', function(request, response, next) {
console.log('body', request.body); // vai chegar aqui o {"password":1138}
console.log(request.body.password); // 1138
response.send('OK!!');
});
Then I used in the terminal the following command:
curl -X POST http://localhost:3000 -H 'Content-Type: application/json' -d '{"password":1138}'
The point is that the line console.log('body', request.body);
returned right the JSON content body { '\'{password:1138}\'': '' }
, but the line console.log(request.body.password);
returned me undefined
.
I got no mistake, the point was that the request.body.password
returned Undefined.
I even changed the line
app.use(bodyParser.urlencoded({
extended: true
}));
for
app.use(bodyParser());
And though the message came:
body-parser deprecated bodyParser: use individual json/urlencoded middlewares
body-parser deprecated Undefined Extended: provide Extended option Node-modules body-parser index.js
Did not present error, but again the request.body.password
appears undefined
Does anyone know what might be going on?
What happens to you if you do
console.log('body', typeof request.body);
?– Sergio
The line "console.log('body', typeof request.body);" appears as a result: body Object
– cmcampos86
Okay, and what gives
console.log('body', Object.keys(request.body));
?– Sergio
Json contents: body [ '{password:1138}'' ]
– cmcampos86
Hmmm... these bars are suspicious! The data is not being converted...
– Sergio
Forehead join
app.use(bodyParser.json());
beforeapp.use(bodyParser.urlencoded({
– Sergio
I’m using the versions below: Express: "version": "4.14.0" body-parser: "version": "1.15.2" I don’t know if this has anything to do with it!
– cmcampos86
I tested your setup and put it together
app.use(bodyParser.json());
works.– Sergio
I used this: var express = require('express'); var app = express(); var bodyParser = require('body-parser'); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ Extended: true }); app.post('/', Function (request, Response, next) { console.log(request.body); console.log(request.body.password); }); app.Listen(3000); console.log('Server running at http://127.0.0.1.: 3000/');
– cmcampos86
Curl in terminal: Curl -X POST http://localhost:3000 -H 'Content-Type: application/json' -d '{"password":1138}' The result remains: { '{password:1138}': '' } /console.log(request.body); Undefined //console.log(request.body.password); Weird!
– cmcampos86
This setting works well for me (on Mac)...
– Sergio
huumm... I’m using Windows 7... I’m going to do a search on this!
– cmcampos86
Thanks for the help!
– cmcampos86
I solved it! The problem was in the way the head was being passed in Curl. Debugging I saw that the header in Nodejs was not like Json and I had passed in Curl In the Curl command I added a -i before -H: Curl -X POST http://localhost:3000 -i -H 'Content-Type: application/json' -d '{password":1138}' Ai the result appeared right on the console.log(request.body); and on the console.log(request.body.password); Thanks for the help!
– cmcampos86
Good! Put as answer :)
– Sergio