Getting an error on making Https request using NodeJS - node.js

I am trying out a NodeJS sample file from a tutorial as I am new to NodeJS but I just can't make it work. I get an error when I run below code:
var https = require("https");
var fs = require("fs");
var options = {
hostname: "en.wikipedia.org",
port: 443,
path: "/wiki/George_Washington",
method: "GET",
}
var req = https.request(options, function(res){ });
req.on('error', function(e) {
console.log(`here comes the error ${e}`);
});
I get the following error:
here comes the error:
Error:101057795:error:140770FC:SSLroutines:SSL23_GET_SERVunknownprotocol:openssl\ssl\s23_clnt.c:794:
I am clueless, I appreciate your help and insight :)
Update: Code had to be modified to go through a proxy server. I posted the solution and findings.

I solved the issue. Here is a summary of the problem and the solution that worked for me.
I ran this code in another network and it worked fine. So I realized the issue is that I am running the code behind our corporate web proxy and I need to modify my code to go through the proxy system instead of making a direct connection to target web server. I tried to install https-proxy-agent module but it failed to install. Again, because I am behind the proxy system. There is a npm config settings file named .npmrc file which can be found under C:/users/[YOUR_USER]. I added below configs to .npmrc to be able to install new packages as was advised Here.
proxy = http://172.26.128.35:3128/
https_proxy = http://172.26.128.35:3128/
strict-ssl = false
ca = null
registry = http://registry.npmjs.org/
Finally I modified my code as below to make it go through and the proxy system and voila~, It worked like a charm. If you encountered this issue, I hope this helps.
var https = require("https");
var fs = require("fs");
var HttpsProxyAgent = require('https-proxy-agent');
var proxy = 'http://172.26.128.35:3128';
var agent = new HttpsProxyAgent(proxy);
var options = {
hostname: "en.wikipedia.org",
port: 443,
path: "/wiki/George_Washington",
method: "GET",
agent: agent
}
var req = https.request(options, function(res) {
console.log(res.statusCode);
res.on('data', function(d) {
process.stdout.write(d);
});
});
req.end();
req.on('error', function(e) {
console.log(`here comes the error ${e}`);
});

Have you tried this code?
var https = require("https");
var fs = require("fs");
var options = {
hostname: "en.wikipedia.org",
port: 443,
path: "/wiki/George_Washington",
method: "GET",
}
var req = https.request(options, function(res) {
console.log(res.statusCode);
res.on('data', function(d) {
process.stdout.write(d);
});
});
req.end();
req.on('error', function(e) {
console.log(`here comes the error ${e}`);
});
Works fine for me!

Related

NodeJS https request failing with Error: socket hang up - code: ECONNRESET - failing on AWS EC2 but working perfectly locally on MacOS

I am using this code to connect to a 3rd party server via HTTP GET. Locally on my MacOS this script works perfectly and I get statusCode:200 together with a valid message from the server. Am I missing something which should be added to this request when connecting from AWS?
const https = require("https");
var fs = require("fs");
var httpsAgent = require("https-agent");
var agent = httpsAgent({
pfx: fs.readFileSync("certs/test.com.pfx"),
passphrase: "xxxxxx",
rejectUnauthorized: true,
//enableTrace: true,
ca: fs.readFileSync("certs/ca-bundle.pem"),
});
const path = "/testapp?application=TEST&method=send&message=TEST"
const options = {
hostname: "test.server.com",
port: 443,
path: path,
method: "GET",
agent: agent,
};
''
console.log("Connecting to: https://test.server.com" + path)
const req = https.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`);
res.on("data", (d) => {
process.stdout.write(d);
});
});
req.on("error", (error) => {
console.error(error);
});
req.end();
Issue Solved: Issue was actually not related to SSL. Packet was being reject to invalid MTU size. Adjusted MTU value and worked as expected.

How to do HTTPS GET with client certificate in node

I can use curl for making a GET request ->
`curl -v https://example.com:82/v1/api?a=b` -E client_cert.pem:password
How can I use same in node. I tried request, superagent but not able to pass certificate.
Thanks in advance!
This worked for me -
var https = require('https');
var fs = require('fs');
var options = {
hostname: 'example.com',
port: 83,
path: '/v1/api?a=b',
method: 'GET',
key: fs.readFileSync('/path/to/private-key/key.pem'),
cert: fs.readFileSync('/path/to/certificate/client_cert.pem'),
passphrase: 'password'
};
var req = https.request(options, function(res) {
console.log(res.statusCode);
res.on('data', function(d) {
process.stdout.write(d);
});
});
req.end()

Error: connect ETIMEDOUT Using node.js to download zip

I am using node.js to download a zip folder from github's server. For some reason the code below works on any other network except for at work. Not sure it's a proxy or firewall issue slowing down the request causing a timeout.
var https = require('https');
var fs = require('fs');
var options = {
hostname : 'codeload.github.com',
port : 443,
path : '/wet-boew/wet-boew/zip/master',
method : 'GET'
};
var file = fs.createWriteStream("test_folder/master.zip");
var req = https.request(options, function(res) {
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
res.on('data', function(d) {
file.write(d);
});
});
req.end();
req.on('error', function(e) {
console.error(e);
});
This is the full error that is generated when running the above code on the network:
{ [Error: connect ETIMEDOUT] code: 'ETIMEDOUT', errno: 'ETIMEDOUT', syscall: 'connect' }
any work around ideas or help is appreciated, thanks!

nodejs make a GET call to an https site

i am using nodejs v0.10.28 on ubuntu 12LTS,
am trying to make a request to https enable site behind a web proxy using this test code:
var http = require("http");
var options = {
host: "mycompany.proxy.host",
port: 8088,
//host: "https://login.launchpad.net",
//port: 443,
//path: "/",
headers: {
Host: "login.launchpad.net"
}
};
http.get(options, function(res) {
console.log(res);
res.pipe(process.stdout);
}).on('error', function(e) {
console.log(e);
});
but am not sure what are the correct option params to use to make a successful HTTP GET
You should be using node https instead of http module for making https requests. Otherwise the server will most likely reset your connection, and an ECONNRESET error will be caught by the error handler.

How turn off curl's verification of the certificate in nodejs?

If I send curl -k "https://abc.com?querystring"
-k option to turn off curl's verification of the certificate
How to do this in nodejs If I want to make a GET request?
How to override all http GET request do it in the same way?
Thank you for your support.
Set the rejectUnauthorized option to false.
var https = require('https');
var req = https.request({
hostname: 'example.com',
port: 443,
path: '/',
method: 'GET',
rejectUnauthorized: false
}, function() { ... });
Check the following code:
var http = require('http');
var target = {
host : 'localhost',
port : 3000,
path : 'URL'
//pfx: Certificate, Private key and CA certificates to use for SSL. Default is null.
//cert: Public x509 certificate to use. Default null.
};
var Req_options = {
host: target.host,
port: target.port,
path: target.path,
agent: false,
method: 'GET'
};
callback = function(response) {
var str = ''
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
console.log(str);
});
}
var req = http.request(Req_options, callback);
req.end();
Updated as per comments:
In the above code, I have changed the https & target only as follows:
var https = require('https');
var target = {
host : 'www.google.co.in',
port : 443,
path : '/'
};
The output is as follows:
</html>
.........
.........
.........
attachEvent&&window.attachEvent("onload",n);google.timers.load.t.prt=e=(new Date).getTime();})();
</script></body></html>
For more information, check this Node.js API docs

Resources