Node.js "request" module problem

Not applicable

We are trying to implement a proxy using Node.JS.

When we try to use the Node.JS "request" module, Apigee gives an error, which you can see from http://org-test.apigee.net.

The code is very simple and similar to the sample (https://github.com/apigee/node-samples/blob/master/node/mashup/mashup.js).

var request = require('request');
var http = require('http');
var urlparse = require('url');
var util = require('util');

function sendError(resp, code, msg) {
var o = { 'error': msg };
resp.writeHead(code, {'Content-Type': 'application/json'});
resp.end(JSON.stringify(o));
}


var svr = http.createServer(function(req, resp) {
var request = require('request');

var options = {
url: 'https://test.com/api/v2/asa',
method:'POST',
headers: {
'Accept': 'application/json',
'Accept-Charset': 'utf-8',
'Api-Key': '33769302e0812dsadasdadd'
},
json:true,
body:{resource:{
group_name:'testing'
}}
};

request.post(options, function (err,res,body) {
console.log(body)

});
resp.writeHead(200, {'Content-Type': 'application/json'});
resp.end(JSON.stringify("{}"));
});

svr.listen(9000, function() {
console.log('Node Mashup sample app is running on port 9000');
});

When I deploy my application, I use the following command:

apigeetool deploynodeapp -u email -p password -o org -e test -n testapp -d . -m app.js

Can you help me with this issue, or guide me to the people that can help me resolve the issue?

Solved Solved
1 12 4,933
1 ACCEPTED SOLUTION

I think this is a compatibility problem, because the current version of the request module requires node version >= 4.

https://github.com/request/request/blob/master/package.json

"node": ">= 4"

But, Edge currently supports Node.js 0.10.32.

https://docs.apigee.com/api-services/content/understanding-edge-support-nodejs-modules

You may consider to use alternative ways to make http request, e. g. 'native' http.request.


UPDATE:

I've checked the GitHub repo for the request module. The last version that was supported on node 0.10.x was version 2.71.0

Therefore, to use the request module on Apigee Edge nodes targets, update your dependencies in package.json like so:

"request": "2.71.0"

https://github.com/request/request/tree/v2.71.0


View solution in original post

12 REPLIES 12

Hi @Uurtsaikh Jamsrandorj, did you include the request module as part of the dependencies in your package.json descriptor file?

Yes, I did.

Here is my package.json file.

{
  "name": "test",
  "version": "0.0.0",
  "description": "test",
  "main": "app.js",
  "private": true,
  "dependencies": { 
    "request": "2.x.x"
  }
}

Hi, this answer is now marked "accepted".

Have you solved your problem? what was the solution?

If not, please provide additional information - specifically what error are you seeing?

no. Can you help me to figure this out?

You wrote:

Apigee gives an error,

What error does Apigee give? Can you tell us exactly the error message you see? And when do you see it - after running which command or request? And where do you see it?

Not applicable

No, I have not solved yet my problem

I think this is a compatibility problem, because the current version of the request module requires node version >= 4.

https://github.com/request/request/blob/master/package.json

"node": ">= 4"

But, Edge currently supports Node.js 0.10.32.

https://docs.apigee.com/api-services/content/understanding-edge-support-nodejs-modules

You may consider to use alternative ways to make http request, e. g. 'native' http.request.


UPDATE:

I've checked the GitHub repo for the request module. The last version that was supported on node 0.10.x was version 2.71.0

Therefore, to use the request module on Apigee Edge nodes targets, update your dependencies in package.json like so:

"request": "2.71.0"

https://github.com/request/request/tree/v2.71.0


Hi Artem Petrosian,

Thank you for your help.

It sounds like your right.

Do you know which version of request module I should use?

What I am trying to do is simple (call external HTTP RESTful web service).

How would you do this one, if you were doing the same thing?

I would like to use Node.js approach, and I am hoping that this option will give me more flexibility.

Thanks,

Tom

Hi , use below mentioned version for request module.

"request": "1.x.x"

Thanks

Anil P

There are variety of different ways to call external service (Service Callout, JavaScript or Node.js). If you prefer Node way you can use build in http module.

https://nodejs.org/docs/latest-v0.10.x/api/http.html#http_http_request_options_callback

or use compatible version of request module (see updated answer).

The body you are trying to post does not appear to be Json serialized as mentioned here.

https://github.com/request/request#requestoptions-callback

That could the problem. Is the code working on your local setup ?

You may also try express .. here is a snippet.

var express = require('express');
var bodyParser = require("body-parser");
var request = require('request');

var app = express();
app.use(bodyParser.json());
app.use (function (error, req, res, next){
    res.status(400).send(error.message);
});

var router = express.Router();
app.use("/",router);

router.get('/', function(req, res){

  var _data = JSON.parse(JSON.stringify({
    resource: {
      group_name: 'testing'
    }
  }));
  
var _url = 'https://test.com/api/v2/asa';
request.post({
          headers: {'content-type': 'application/json', 'Accept-Charset': 'utf-8', 'Api-Key': '33769302e0812dsadasdadd' },
          url: _url,
          json: _data
      }, function (error, response, body) {
          if (!error) {
            console.log(body);
          }
      });

      res.send("Hello");
});


app.listen(9000);

******************************************

{
  "name": "community",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.18.2",
    "express": "^4.16.2"
  }
}