I am making a REST API call from my php and Node.js application to a particular URL provided by the client which returns a Json object. It works fine from with the PHP. However, I am unable to receive data from my node application? What might be the possible reason can someone help me ?
Note: I have pasted a dummy REST URI for security reasons
It works fine with PHP infact i get the json formatted data in like couple of seconds.
$response =
file_get_contents('http://xyz.net/v2_resmgr/providers/pools'); echo
$response;
I try the same url using node.js i get a TimeOut error. I also tried setting the timeout but it would still not work.
var job = new CronJob({
cronTime: '0 */3 * * * *',
onTick: function () {
url= "http://xyznet/v2_resmgr/providers/pools";
var request = http.get(url, function (response) {
var buffer = "",
data,
route;
response.on("data", function (chunk) {
buffer += chunk;
});
response.on("end", function (err) {
console.log(buffer);
});
request.setTimeout( 200000, function( ) {
// handle timeout here
console.log("Time Out call to the Rest API");
});
});
},
start: true
});
job.start();
I don't know if this is the answer you are looking for, but life gets easier when you use the 'request' package (https://www.npmjs.org/package/request)
Here is what the code would look like using the request module:
var request = require('request');
request('http://xyznet/v2_resmgr/providers/pools', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Print the body of the response.
}
})
Update: I coded something a little closer to your post. The code below does not use the "request" module and it contacts the server every 3 seconds.
setInterval(function () {
http.get('http://echo.jsontest.com/key/value', function (response) {
var responseBody = '';
response.on('data', function (chunk) {
responseBody += chunk;
});
response.on('end', function () {
console.log(responseBody);
var object = JSON.parse(responseBody)
});
});
}, 3000);
Related
I am looking for the Node.js of the following PHP Script:
$SMA_APICall = "https://www.alphavantage.co/query?function=SMA&symbol=".$symbolValue."&interval=15min&time_period=10&series_type=close&apikey=R3MGTYHWHQ2LXMRS";
$SMAresponse = file_get_contents($SMA_APICall);
$jsonSMA = json_encode( $SMAresponse);
Here, I am trying to make a call to an API. The API call returns a json object.
I want to repeat the same thing using Node js
I believe what you're trying to do is making a request to an API and get the JSON data.
Here's how you can do it with native Node.js module https
const https = require('https');
https.get(`https://www.alphavantage.co/query?function=SMA&symbol=${symbolValue}&interval=15min&time_period=10&series_type=close&apikey=R3MGTYHWHQ2LXMRS`, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
console.log(JSON.parse(data)); // JSON Data Here
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
There're several other ways you can do this with other simpler packages. I highly recommend axios because it's cleaner and easier.
The full examples please refer to this article
Take a look at the request library: https://github.com/request/request
var request = require('request');
var url = "https://www.alphavantage.co/query?function=SMA&symbol=" + symbolValue + "&interval=15min&time_period=10&series_type=close&apikey=R3MGTYHWHQ2LXMRS";
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var jsonSMA = JSON.parse(body);
// Continue code here
}
});
I get weird characters when i am trying to parse a page.
Here is my code:
var getPageContent = function getPageContent(url, callback) {
https.get(url, function (res) {
var data = "";
res.on('data', function (chunk) {
data += chunk;
});
res.on("end", function () {
callback(data));
});
}).on("error", function () {
callback(null);
});
};
getPageContent(url, function (response) {
var $ = cheerio.load(response, { decodeEntities: false });
$("div.details-info").each(function() {
console.log($(this).html());
});
});
My result is:
<span>Ст��атегии</span>
<span>Стратег��и</span>
<span>Стра��егии</span>
<span>Стратегии</span>
<span>Стратегии</span>
...
The strangest thing is that from same url, sometimes i get this strange characters, sometimes i don't. And also when i am running this from my computer it's working fine. I get this characters on server only.
You will probably need to manually convert the charset of response to UTF-8. You can do this using the iconv or iconv-lite modules. cheerio itself does not automatically handle charset conversions.
I'm writing a REST client to access a REST server that paginates the replies. I've written something like the below, using Node's HTTPS library:
var nextPage = true, pageNo=0;
do {
// builds the url
options.path = "?page=" + pageNo;
var req = HTTPS.request(options, function(res) {
res.on('data', function (chunk) {
parser(chunk, function(err, result) {
// do something with result
if (!("nextPage" in result)) {
nextPage = false;
}
});
});
});
// handle errors and end request
pageNo++;
} while(nextPage)
This of course doesn't work, because the request to the server is handled asynchronously and the nextPage variable is never updated, but I can't figure out a way to make it work using higher-order functions.
Should I wrap the request around a recursive function, passing the state every time? Or is there a better way?
UPDATE 1:
Do you mean something like this?
var nextPage = true, pageNo=0;
var async = require('async');
async.doWhilst ( function () {
// builds the url
options.path = "?page=" + pageNo;
var req = HTTPS.request(options, function(res) {...});
// handle errors and end request
pageNo++;
}, function () {return nextPage;}, function () {});
It does not work either, what am I doing wrong?
OK, I must be dense since I cannot find anywhere how to get the error status codes when using Node.JS http.get or http.request.
My code:
var deferred = $q.defer();
var req = https.get(options, function(response) {
var str = '';
response.on('data', function(chunk) {
str += chunk;
});
response.on('end', function() {
console.log("[evfService] Got user info: " + str);
deferred.resolve(str);
});
});
req.on('error', function(e) {
deferred.reject(e);
});
In that "req.on" bit, what I want is the http status code (i.e. 401, 403, etc.). What I get is a semi-useless error object that does not give me the code or any reference to the response object.
I have tried intercepting in the function(response) callback, but when there is a 404, it never gets called.
Thanks!
Your callback gets called regardless of the response status code from the server, so within your callback, check response.statusCode. That is, a 4xx status code isn't an error at the level you're working at; the server responded, it's just that the server responded by saying the resource wasn't available (etc.)
This is in the documentation but characteristically vague. Here's the example they give, with a comment pointing to the relevant bit:
var https = require('https');
https.get('https://encrypted.google.com/', function(res) {
console.log("statusCode: ", res.statusCode); // <======= Here's the status code
console.log("headers: ", res.headers);
res.on('data', function(d) {
process.stdout.write(d);
});
}).on('error', function(e) {
console.error(e);
});
If you try that with (say) an unknown resource, you'll see statusCode: 404.
So for what you're doing, you may want something like this:
var deferred = $q.defer();
var req = https.get(options, function (response) {
var str = "";
if (response.statusCode < 200 || response.statusCode > 299) { // (I don"t know if the 3xx responses come here, if so you"ll want to handle them appropriately
response.on("data", function() { } ); // ¹
deferred.reject(/*...with appropriate information, including statusCode if you like...*/);
}
else {
response.on("data", function (chunk) {
str += chunk;
});
response.on("end", function () {
console.log("[evfService] Got user info: " + str);
deferred.resolve(str);
});
}
});
req.on("error", function (e) {
deferred.reject(/*...with appropriate information, but status code is irrelevant [there isn"t one]...*/);
});
¹ The empty data event handler in the branch handling non-OK status codes is there because of this note in the documentation:
...if a 'response' event handler is added, then the data from the response object must be consumed, either by calling response.read() whenever there is a 'readable' event, or by adding a 'data' handler, or by calling the .resume() method. Until the data is consumed, the 'end' event will not fire. Also, until the data is read it will consume memory that can eventually lead to a 'process out of memory' error.
Since we're passing a function to https.get, we're hooking the 'response' event, which suggests we need to do one of those things (in this case, I've added a do-nothing data handler). Thanks to Nicolas2bert for pointing that out!.
An error code 400 response is not considered an error by node.js.
Try response.statusCode in this:
request.on('response', function (response) {});
Here's a very small example how to get the error code. Just change the https to http and create an error:
var https = require('https')
var username = "monajalal3"
var request = https.get("https://teamtreehouse.com/" + username +".json", function (response) {
console.log(response.statusCode);
});
request.on("error", function (error) {
console.error(error.status);
});
I am trying to make a function that returns the content of the webpage and this is what I have so far
var get_contents = function() {
var httpRequestParams =
{
host: "google.com",
port: 80,
path: "/?"
};
var req = http.get(httpRequestParams, function(res)
{
var data = '';
res.on('data', function(chunk) {
data += chunk.toString();
});
//console.log(data);
}).end();
return req;
}
This when I run this code, I see the html contents when the console logging is turned on but when I try to return the output, it just never works.
I can't figure out a way to return get_contents() anywhere. On the console, it just doesnt respond.
Thanks
Something like that: (dont forget to handle error and timeout)
var on_contents = function(cb) {
var httpRequestParams =
{
host: "google.com",
port: 80,
path: "/?"
};
var req = http.get(httpRequestParams, function(res)
{
var data = '';
res.on('data', function(chunk) {
data += chunk.toString();
});
res.on('end', function(){
cb(data);
});
//console.log(data);
}).end();
}
function onFinish(data) {
console.log(data);
}
on_contents(onFinish)
The short answer is: You can't return the data from that function. http.get is asynchronous, so it doesn't actually start running the callback until after your function ends. You'll need to have your get_contents function take a callback itself, check in the http.get handler whether you're done loading and, if you are, call the get_contents callback.
There is an awesome module [request][1] available in node.js.
var request = require('request'),
url = require('url');
var server = http.createServer(function (request, response) {
getPage("http://isohunt.com/torrents/?iht=-1&ihq=life+is+beautiful", function (body) {
console.log(body);
})
});
server.listen(3000);
More information can be found on http://www.catonmat.net/blog/nodejs-modules-request/