How to read request payload in node.js?

Please let me know how to read request data in node.js server .

1 Like

Reading request payload in Apigee Edge is no different from reading the payload in a regular node.js application. Here is an example of reading payload info.

1 Like

You can read the payload using apigee-access module. You can learn more about it here. Using apigee-access you can read/write any variable defined in EDGE.

1 Like

Thankks, But apigee-access module is not supporting in out environment is there any other way to read request payload in node.js . Ex. When we send a post request to node.js it should read the data in the payload.

By the way I had also posted many queries regarding the nodeJS and BaaS earlier. I tried it just now what @sudheendra1 has mentioned above but it does not work for Simple GET request too.

Here is the error which I am getting.

D:\Node Sample\node_modules\usergrid\lib\usergrid.js:112
      r.body = r.body || {};
                ^
TypeError: Cannot read property 'body' of undefined
    at Request._callback (D:\Node Sample\node_modules\usergrid\lib\usergrid.js:1
12:17)
    at self.callback (D:\Node Sample\node_modules\usergrid\node_modules\request\

I believe this error is from the usergrid itself. Any pointers in this regard? The same get works when I use usergrid request instead of createCollection and running entity specific functions.

1 Like

@Kodandapani You are using Apigee on-premise ? And are you running a more than 6 month old build ? Because apigee-access module has been made available for atleast that long I think.

I tried on a free org the following code and it is working for me :

 var apigee = require('apigee-access'); 

 http.createServer(function (request, response) { 

 var receivedMessage = apigee.getVariable(request, 'request.content');

}).listen(8124);

Is this not working for you ?

1 Like

Thanks, but apigee-access module is not supporting in our environment. Our environments are on-premise.Please let me know is there any other way to read data in node.js

Can you please share the entire code?

@Kodandapani The example I shared above does not require “apigee-access” module. Can you try that? What version of the OPDK are you using?

Here is the code with ug as usergrid client.

app.get('/jokecomments', function(req, res) {
       ug.createCollection({ type: 'jokecomment' }, function(err, comments) {
        if (err) {
          res.jsonp(500, {'error': JSON.stringify(err) });
          return;
        }
        var jc = [];
        while (comments.hasNextEntity()) {
          var entity = comments.getNextEntity().get();
          var c = { 'comment': entity.comment,
                    'commentor': entity.commentor,
                    'jokeid': entity.jokeid,
'uuid': entity.uuid};
          jc.push(c);
        }
        res.jsonp(jc);
    });
});

AFAIK , you need express/apigee-acces or such modules if we need to get the payload directly but if we want to use node.js to parse the req the only way is as below.

//HTTP | Node.js v24.4.1 Documentation

var svr = http.createServer(function(req, resp) {
   var body = "";
  req.on('data', function (chunk) {
    body += chunk;
  });
  req.on('end', function () {
    console.log('body: ' + body);
    var jsonObj = JSON.parse(body);
  console.log(jsonObj.test);
  })
    resp.end('Hello, World!');
});

The problem that pani has is, he is on a release which supports node.js but not apigee-access-module.

@sudheendra1 , @sarthak , Am I missing anything or do you see any issues with the above code to parse request body ?

@Maruti Chand You are right, apigee-access module is need only to get access to API proxy flow variables and caches from within Node.js application code. Otherwise you can simply use express to access request payload.

How are you fetching “comments”?

Also it should “post”, instead of “get” since you are creating an entity.

Thanks Maruthi and Sudeendra, now we are accessing request payload in node.js by using the above code

Hey its a get.

Comments are already there. I referred the sample which you have suggested.

May be we should have a closer look at you setup. Please go ahead and raise a support ticket - http://apigee.com/about/support/portal

@sudheendra1

This is the error I am getting

D:\Node Sample\node_modules\usergrid\lib\usergrid.js:112
      r.body = r.body || {};
                ^
TypeError: Cannot read property 'body' of undefined
    at Request._callback (D:\Node Sample\node_modules\usergrid\lib\usergrid.js:1
12:17)

It is actually strange at line 112 where ‘r’ is passed in callback but it is nowhere present in same local function.