Curl + HTTP Nodejs Server

Asked

Viewed 246 times

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);?

  • The line "console.log('body', typeof request.body);" appears as a result: body Object

  • Okay, and what gives console.log('body', Object.keys(request.body));?

  • Json contents: body [ '{password:1138}'' ]

  • Hmmm... these bars are suspicious! The data is not being converted...

  • Forehead join app.use(bodyParser.json()); before app.use(bodyParser.urlencoded({

  • 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!

  • I tested your setup and put it together app.use(bodyParser.json()); works.

  • 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/');

  • 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!

  • This setting works well for me (on Mac)...

  • huumm... I’m using Windows 7... I’m going to do a search on this!

  • Thanks for the help!

  • 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!

  • Good! Put as answer :)

Show 10 more comments

1 answer

1


I resolved!

The problem was in the way that the header was being passed on Curl. Debugging I saw that the header in Node.js was not as JSON and I had passed in Curl.

In the Curl command I added a -i before the -H:

curl -X POST localhost:3000 -i -H 'Content-Type: application/json' -d '{"password":1138}' 

The result appeared right on console.log(request.body); and in the console.log(request.body.password);

Thanks for the help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.