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()
Related
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.
can we replicate curl resolve host in node-fetch or any other node http library.
curl https://www.example.net --resolve www.example.net:443:127.0.0.1
You don't need another lib to do this. The builtin http module works fine:
const http = require('http');
const options = {
hostname: '2606:2800:220:1:248:1893:25c8:1946',
port: 80,
path: '/',
method: 'GET',
headers: {
'Host': 'example.com'
}
};
const req = http.request(options, (res) => {
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
res.on('end', () => {
console.log('No more data in response.');
});
});
req.end();
In HTTP protocol, the host name is in headers, and the IP used to establish the TCP connection is independent from that.
Example Program:
Server:
var fs = require('fs');
var https = require('https');
var options = {
key: fs.readFileSync('server-key.pem'),
cert: fs.readFileSync('server-crt.pem'),
ca: fs.readFileSync('ca-crt.pem'),
};
https.createServer(options, function (req, res) {
console.log(new Date()+' '+
req.connection.remoteAddress+' '+
req.method+' '+req.url);
res.writeHead(200);
res.end("hello world\n");
}).listen(4433);
Client:
var fs = require('fs');
var https = require('https');
var options = {
hostname: 'localhost',
port: 4433,
path: '/',
method: 'GET',
ca: fs.readFileSync('ca-crt.pem')
};
var req = https.request(options, function(res) {
res.on('data', function(data) {
process.stdout.write(data);
});
});
req.end();
I have generated the Keys and certificate using openssl in my Linux server.
But while running client program its showing as Error: self signed certificate . By referring some websites and even stack overflow discussions some have mentioned that using a option called rejectUnauthorized: false even though there is no use in using this parameter while using certificates for secure transfer of data.
Is there any way to trust the certificates in Linux server?
Any example program with certificates and node JS Program ?
Node JS Client to connect to server?
Without Using rejectUnauthorized: false?
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!
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