1

Sample 'Advanced REST Client' Request

I'm using Postman and Advanced REST client to create a basic POST request for the following code -

'use strict';
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var http = require('http');

// configure the app to use bodyParser()
app.use(bodyParser.urlencoded({
    extended: false
}));
app.use(bodyParser.json());
//app.listen(6666);

http.createServer(function (req, res) {
    h2s(req, res);
}).listen(6666, '127.0.0.1');

console.log('Server running at http://127.0.0.1:6666/');

module.exports = function h2s(req, res) {
    console.log("inside h2s");
    app.use(function (req, res) {
        console.log("req.body : " + req.body);
        res.send("OK");
    });
}

But, when I debug, I find that req.body is missing in the "req object tree". What's more strange is all the changes that I make to req.headers are available in the req object tree.

Looks like I seem to be making a trivial mistake, but I'm unable to figure it out. Been trouble-shooting for an hour or so but no luck!

Can anyone of you figure out why req.body seems to be missing from the req object tree?

Will be of great help to me. Thanks!

Sandy
  • 61
  • 8

3 Answers3

0

It looks like you have several issues in your code:

instead of

http.createServer(function (req, res) {
    h2s(req, res);
 }).listen(6666, '127.0.0.1');

console.log('Server running at http://127.0.0.1:6666/');

module.exports = function h2s(req, res) {
    console.log("inside h2s");
    app.use(function (req, res) {
    console.log("req.body : " + req.body);
    res.send("OK");
   });
}

For creating the server, try

http.createServer(app).listen(8000, '127.0.0.1'); //using http

Or (using express directly)

app.listen(8000,function(){
    console.log('Server running at http://127.0.0.1:8000/');
});

Then register an handler function for your requests, there you can access req.body

app.use(function (req, res) {
    console.log("req.body : " + req.body);
    res.send("OK");
});
Cz01
  • 68
  • 7
  • Thanks! That helped with getting a 200 OK response. Further, how can I add it to a function? I want to segregate them under functions. – Sandy Jun 13 '17 at 09:25
  • @Sandy which part would you like to do in a separate function? there isn't much functionality in the code yet, you're just creating a server, and registering an handler function to handle your request. Of course if you want you can name the function,something like function reqHandler (req, res) { console.log("req.body : " + req.body); res.send("OK"); } And register it like this app.use(reqHandler) – Cz01 Jun 13 '17 at 09:30
  • Haha! This is just an extract of the code. I meant, the code where we handle the req. Thanks! – Sandy Jun 13 '17 at 10:28
  • @Sandy is the answer enough? can you accept the answer? – Cz01 Jun 13 '17 at 10:50
  • Thanks @Cz01. Yes, indeed! – Sandy Jun 14 '17 at 11:58
0

Dear u set body parser Url Encoding to true

// configure the app to use bodyParser()
app.use(bodyParser.urlencoded({
    extended: true
}));

and check by printing the req.body, it work for me and might for u also

Ratan Uday Kumar
  • 5,738
  • 6
  • 35
  • 54
-1

the req.body can be accessed also when

content-type:"application/x-www-form-urlencoded"

read this
In your case , your content-type is application/json"

so try changing the content-type to "application/x-www-form-urlencoded"

also url encode the parameters while sending to the server from JS

also solution can be

// fire request
request({
    url: url,
    method: "POST",
    json: true,
    headers: {
        "content-type": "application/json",
    },
    body: JSON.stringify(requestData)
}, ...
manish kumar
  • 4,412
  • 4
  • 34
  • 51