Call https web service in node.js (per proxy)

I am trying to call the https web service from node.js. I am behind the proxy server, so I provide the proxy and port in the settings along with the credentials. But I get the following error:

[Error: 2060:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol :openssl\ssl\s23_clnt.c:683: ]

Below is a snippet of code in which I am trying to call the https web service:

 var https = require('https'); var username = 'username'; var password = 'password'; var auth = 'Basic ' + new Buffer(username + ':' + password).toString('base64'); var data = ''; var options = { hostname: 'proxy', port: 8080, path: 'https://crm.zoho.com/crm/private/json/Leads/getMyRecords?authtoken=11111tsvs26677026bcba45ae3f&scope=crmapi', headers: { "Proxy-Authorization" : auth } }; console.log("options- " + JSON.stringify(options) + '\n'); var req = https.request(options, function(res) { console.log("statusCode- " + res.statusCode); console.log("headers-" + res.headers); res.on('data', function(chunk) { data += chunk; }); res.on('end', function (chunk) { console.log('response-' + data); }); }); req.end(); req.on('error', function(e) { console.error(e); }); 

Can anyone help me solve this problem?

Thanks in advance, Manoj

+4
source share
1 answer

I had the same problem by mistake using

 var https = require('https'); 

But for my proxy, only HTTP is required, the proxy server will be responsible for completing the HTTPS request, because you set it according to the path parameter

 (...) path: 'https://crm.zoho.com/crm/private/json/Leads/getMyRecords?authtoken=11111tsvs26677026bcba45ae3f&scope=crmapi', (...) 

This works for me:

 // baiken9: use http here var http = require('http'); var username = 'username'; var password = 'password'; var auth = 'Basic ' + new Buffer(username + ':' + password).toString('base64'); var data = ''; var options = { hostname: 'proxy', // baiken9: Proxy Port port: 8080, // baiken9: Add method type in my case works using POST method: "POST", // baiken9: when proxy redirect you request will use https. It is correct as is path: 'https://crm.zoho.com/crm/private/json/Leads/getMyRecords?authtoken=11111tsvs26677026bcba45ae3f&scope=crmapi', headers: { // baiken9: I cannot test it my proxy not need authorization "Proxy-Authorization" : auth, // baiken9: required for redirection host: "crm.zoho.com", // baiken9: length of data to send 'content-length': 0 } }; console.log("options- " + JSON.stringify(options) + '\n'); var req = http.request(options, function(res) { console.log("statusCode- " + res.statusCode); console.log("headers-" + res.headers); res.on('data', function(chunk) { data += chunk; }); res.on('end', function(chunk) { console.log('response-' + data); }); }); req.end(); req.on('error', function(e) { console.error(e); }); 

Ouput:

 statusCode- 200 headers-[object Object] response-{"response":{"error":{"message":"Invalid Ticket Id","code":"4834"}}} 

Yours faithfully

+4
source

Source: https://habr.com/ru/post/1479891/


All Articles