I want to use the needle module for node.js in streaming mode, similar to this example from the needle docs:
var stream = needle.get('http://www.as35662.net/100.log');
stream.on('readable', function() {
var chunk;
while (chunk = this.read()) {
console.log('got data: ', chunk);
}
});
This allows me to read the response body from the stream.
How can I access the response headers?
From reading the source, needle emits two events, header and headers.
Interested in headers only:
var stream = needle.get(someURL);
stream.on('headers', function(headers) {
// do something with the headers
});
or status code and headers:
stream.on('header', function(statusCode, headers) {
if (statusCode != 200) {
// scream and panic
}
});
You can read the header before stream starts if you want to.
var needle = require('needle');
var url = 'http://www.stackoverflow.com';
needle.head(url, {method: 'HEAD'}, function (err, response) {
if (!err && response.statusCode == 200) {
console.log((JSON.stringify(response.headers)));
}
});
Or in Request
var request = require('request');
var url = 'http://www.stackoverflow.com';
request(url, {method: 'HEAD'}, function (err, response) {
if (!err && response.statusCode == 200) {
console.log((JSON.stringify(response.headers)));
}
});
Otherwise you can read it after stream.
var needle = require('needle');
var url = 'http://www.stackoverflow.com';
var stream = needle.get(url, function (err, response) {
if (!err && response.statusCode == 200)
console.log((JSON.stringify(response.headers)));
});
But this is also valid for request.
var request = require('request');
var url = 'http://www.stackoverflow.com';
var stream = request.get(url, function (err, response) {
if (!err && response.statusCode == 200)
console.log((JSON.stringify(response.headers)));
});
Related
I have created one NODE JS URL request to get JSON and pass it to the new variable var1.
Var1 can get data within request function when call var1 outside of request it returns null value why? how to get data from URL request and pass it to a new variable outside the Request URL?
function datadata() {
var var1 = 0;
var request = require('request');
request('http://x.x.x.x/json', function (error, response, body) {
if (!error && response.statusCode == 200) {
var importedJSON = JSON.parse(body);
var1 = importedJSON.result.data;
// show value of var1
console.log(var1);
}
});
// cannot show value of var1
console.log(var1);
return var1;
}
function datadata(){
var var1 = 0;
var request = require('request');
return new Promise(function(resolve, reject){
request('http://x.x.x.x/json', function (error, response, body) {
if (err) return reject(err);
try {
if (!error && response.statusCode == 200) {
var importedJSON = JSON.parse(body);
var1 = importedJSON.result.data;
console.log(var1);
} catch(e) {
reject(e);
}
});
});
}
datadata().then(function(res) {
console.log(res);
}).catch(function(err) {
console.err(err);
});
you can't get something outside the request that easy anyway you can try to use Promise
Example
const request = require('request');
var p = new Promise((resolve, reject) => {
request('http://x.x.x.x/json', function (error, response, body) {
if (!error && response.statusCode == 200) {
var importedJSON = JSON.parse(body);
resolve(importedJSON.result.data)
}
});
p.then((data) => {
console.log(data);
})
You can use a callback to the datadata like so
Check whether the body is found on the response, in that case change your code to
request('http://x.x.x.x/json', function (error,response) {
and
var importedJSON = JSON.parse(response.body);
You should do this because your importedJSON may not be having data because according to your code it's data comes from (body)
Mostly likely the json data will be found on the response.body.
Then we callback the specific data you want, in your case it's result.data. Again make sure the result.data can be found on the raw json data that the request gives you otherwise it won't output anything because there's nothing to output, most likely you will get errors
function datadata() {
var var1 = 0;
var request = require('request');
request('http://x.x.x.x/json', function (error,body,response) {
if (error && response.statusCode == 200) {
callback("There was an error")
}else{
var importedJSON = JSON.parse(body);
callback(importedJSON.result.data);
// show value of var1
console.log(var1);
});
}
datadata((error, var1) => {
if(error){
return console.log(error)
}
console.log(var1
})
I'd like to read some information from an remote CSV file using a callback function. Not sure, how exactly to do this.
function:
function getRoomsFromCSV(allRoomsArray) {
var request = require('request');
request('http://localhost:3333/rooms.csv', function (error, response, body) {
if (!error && response.statusCode == 200) {
...
allRoomsText = allRoomsText.substr(0,allRoomsText.length-1) + ']}';
var allRoomsArray = JSON.parse(allRoomsText);
}
})
}
I'd like to call the function and loop through the result array.
var rooms = [];
getRoomsFromCSV( function (rooms) {
for(var i = 0; i < rooms.length; i++) {
console.log("i:",i);
}
However, the for loop is never called and the result (room) seems to be empty.
Try like this
function getRoomsFromCSV(allRoomsArray) {
var request = require('request');
request('http://localhost:3333/rooms.csv', function (error, response, body) {
if (!error && response.statusCode == 200) {
...
allRoomsText = allRoomsText.substr(0,allRoomsText.length-1) + ']}';
allRoomsArray(JSON.parse(allRoomsText)); //response params to callback
}
})
}
You sent callback to retrieve response. so call that callback inside the async function
I have my code trying to execute my request method, but it's not going in there. I'm setting up my objects correctly, but what I log doesn't get executed. Am I setting it up incorrectly?
req = require("request");
var headers = {
"Content-Type": "json/application",
"Authorization": "M6V9Jt2HJa8bcYCjd1xItrVaw1dcHDEY5FRpxdfojhI"
}
var options = {
url: "'http://api.nytimes.com/svc/topstories/v1/home.json?api-key="+NYTIMESKEY,
method: 'GET',
headers: headers
}
for(var i = 0; i < parsed.results.length; i++){
abstracts.push(parsed.results[i].abstract);
var url = formatUrl(abst, "https://api.datamarket.azure.com/data.ashx/amla/text-analytics/v1/GetSentiment?Text=");
var sentStr = '';
console.log("in there");
req(options, function(error, response, body){
if (!error && response.statusCode == 200){
console.log(body);
console.log("executing thing");
}
});
}
First of all, you have a misprint in your options object.
var options = {
url: "'http://api.nytimes.com/svc/topstories/v1/home.json?api key="+NYTIMESKEY,
"url" param has extra single quote. When you try to call req function with current url, you'll get "Type error" (invalid protocol).
Secondly, the reason your function doesn't call, could be empty parsed.results array. I've simplified a bit your code. And now req function is called.
req = require("request");
var headers = {
"Content-Type": "json/application",
"Authorization": "M6V9Jt2HJa8bcYCjd1xItrVaw1dcHDEY5FRpxdfojhI"
}
var options = {
url: "http://api.nytimes.com/svc/topstories/v1/home.json?api-key=" + "something_you_nedd",
method: 'GET',
headers: headers
}
var sentStr = '';
console.log("in there");
req(options, function(error, response, body){
console.log("on answer");
if (!error && response.statusCode == 200){
console.log(body);
console.log("executing thing");
}
});
I'm struggling with working out how to get the return data from this code example. If I try to get the cert data from the callback function its always empty. Is there something I'm missing here?
var Request = require('request');
function callhttp(host) {
var cert = " ";
var options = {
url: 'https://' + host
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
var cert = response.connection.getPeerCertificate();
}
}
Request(options, callback);
return cert
}
console.log(callhttp("www.google.com"));
Best Regards.
By returning 'cert' you refer to the operation as a sync one, which is not.
The correct pattern is to pass a callback function and handle the data whitin:
var Request = require('request');
function callhttp(host, cb) {
var cert = " ";
var options = {
url: 'https://' + host
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
var cert = response.connection.getPeerCertificate();
cb(cert);
}
}
Request(options, callback);
}
callhttp("www.google.com", function(_cert) {
console.log(_cert);
});
Using Request and Express, how do I access the result of my http request for the purpose of rendering it?
var request = require('request');
var http = require('http');
exports.index = function(req, res){
var apiUrl = 'http://api.bitcoincharts.com/v1/weighted_prices.json';
request(apiUrl, function(err, res, data) {
if (!err && res.statusCode == 200) {
data = JSON.parse(data);
console.log(data);
res.render('index', { data: data });
}
});
};
As it is, the res I'm referring to within the request callback is the raw response object and I'm wondering how to call the response from my exports.index function without the request being inaccessible.
Just rename one of the arguments:
// either this:
exports.index = function(req, response) {
...
response.render(...);
};
// or this:
request(apiUrl, function(err, response, data) {
if (!err && response.statusCode == 200) {
data = JSON.parse(data);
console.log(data);
res.render('index', { data: data });
}
};