node request module - sample request failing - node.js

I'm currently using node 7.6.0 and am trying the npm request module 2.80.0. The module is installed via package.json npm install. Following their simple example I immediately get: "Failed: Cannot set property 'domain' of undefined". Its a straight copy paste except for the require part.
var request = require('../node_modules/request/request');
request('http://www.google.com', function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
});
Am I missing something or is there other dependencies I'm not aware of?

I required it incorrectly. Should just be node_modules/request. Confusing since there is an actual request.js inside the request folder with a module exports.
// Exports
Request.prototype.toJSON = requestToJSON
module.exports = Request

Related

How to get the JSON response from newman

I tried to run the end point which is running fine on postman and I just exported as collection and running it through newman on JENKINS CI.
Command:
newman run <POSTMAN_COLLECTION>.json -r json,cli
I'm getting the response.json file in the current directory as like below file:
I'm not able to see the response body inside the json file.
I'm googled but no luck. Is there anyway to get the response body for this postmand_collection? how can I achieve this?
I just want to get the response body as json file and I need to use it as request for other service.
You could create a mini project and use Newman as a library and run it using a script. That way you could use the node fs module to write the response out to a file.
const newman = require('newman'),
fs = require('fs');
newman.run({
collection: '<Your Collection>'
}).on('request', function (error, data) {
if (error) {
console.error(error);
}
else {
fs.writeFile(`response.json`, data.response.stream.toString(), function (error) {
if (error) {
console.error(error);
}
});
}
});
This script is using the Newman .on('request') event, that will extract that information. If you wanted all the response bodies, you may need to modify this slightly and maybe use the appendFileSync to capture all the responses from the requests in the collection.

Javascript object is undefined, even though it is clearly there

I use a get request (using request npm) to access facebook profile info for my bot, and it clearly gets a body but when I try to access first name in the body, it then says that it is undefined.
The code is as such:
request(`https://graph.facebook.com/${sender_psid}?fields=first_name,last_name,profile_pic&access_token=${PAGE_ACCESS_TOKEN}`, function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body);
console.log(body["first_name"]);
console.log(body.first_name);
The expected output is the body and then Jake, Jake but the output from Heroku can be seen below:
2018-12-23T19:06:47.739490+00:00 app[web.1]: body: {"first_name":"Jake","last_name":"Walker","profile_pic":"https://platform-lookaside.fbsbx.com/platform/profilepic/?psid=XXXXXXX","id":"XXXXXXXXX"}
2018-12-23T19:06:47.739542+00:00 app[web.1]: undefined
2018-12-23T19:06:47.739603+00:00 app[web.1]: undefined
The problem was that facebook was providing a JSON string, but my code was trying to access it as if it was a Javascript object as pointed out by luschn.
The fix to this is adapting the code to using JSON.parse(body) which then converts the JSON string to a Javascript object, which can be accessed through the method I was originally trying.

timeout error handling with node.js and express

In my app using node.js and Express I've got a problem with handling timeout error during my request to some 3rd party shop's url. Sometimes shop's server is overloaded and I get a timeout error.
That's my code:
request(url, function (error, response, html) {
console.log("response.statusCode: " + response.statusCode);
if (!error && response.statusCode == 200) {
console.log("OK");
//some code
}
});
I managed to handle errors like 404 but I don't know how to handle timeout connection error. I've read about connect-timeout module for node.js, but I think it is more for for 'in app' requests, rather than for request to 3rd party url. Do I think correctly? What does time after which I get timeout error depends on?

Get request object in Request module

With the Node JS request module it is possible to get the response, but, is there any way of getting the request headers sent?
I'm not sure what the official way of doing this is but there are several things that seem to work.
If you haven't bound the callback to another this value then it will just be the request, e.g.:
request.get(options, function() {
console.log(this.getHeader('... header name ...'));
console.log(this.headers);
});
You could also access the request using response.request:
request.get(options, function(err, response) {
console.log(response.request.getHeader('... header name ...'));
console.log(response.request.headers);
});
That second approach should work anywhere that you have access to the response.
I believe these are the relevant lines in the source code:
https://github.com/request/request/blob/253c5e507ddb95dd88622087b6387655bd0ff935/request.js#L940
https://github.com/request/request/blob/253c5e507ddb95dd88622087b6387655bd0ff935/request.js#L1314

596 Service not found while using ESPN API

I am using this API to consume a ESPN API:
http://api.espn.com/v1/now?apikey=d4skkma8kt2ac8tqbusz38w6
In Node.js, using node-curl library, my snippet looks like this:
var Curl = require('node-curl/lib/Curl')
curl.setopt('URL', url);
curl.setopt('CONNECTTIMEOUT', 2);
curl.on('data', function(chunk) {
console.log(chunk);
});
But everytime when I run this code I keep on getting response as:
<h1>596 Service Not Found</h1>
Strange is, same URL if I hit from the browser I get the correct response so URL is not invalid. This happens only if I try to call from Node.js. Can anyone guide me how to resolve this error? I tried encoding/decoding url, still I get same response.
Also basically I am avoiding any vendor specific libraries as much as possible, since we should have generic api calling framework
You could use request, which is a very popular module. Here's an example of how you could do it:
var request = require('request');
var url = 'http://api.espn.com/v1/now?apikey=d4skkma8kt2ac8tqbusz38w6';
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(JSON.parse(body));
}
});

Resources