I am converting this below ajax 'POST' to Node and can't get past this error message. I have looked at other threads like this How to avoid a NodeJS ECONNRESET error? but doesn't seem to apply. I am posting header basic authentication and getting back a token. Anyone know or come into this issue before?
The error message:
Error: read ECONNRESET at exports._errnoException (util.js:1018:11) at TLSWrap.onread (net.js:568:26)
Below is the ajax call that works:
function createAuth(user, pass) {
var token = user + ":" + pass;
var hash = btoa(token);
return "Basic " + hash;
}
var username = "username";
var password = "password";
$.ajax({
type: "POST",
url: "https://apiserviceprovider/api/login",
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", createAuth(username, password));
},
success: function(data) {
sessionStorage.setItem("mytoken", data.token);
},
});
Below is the Nodejs Code:
var user = 'username';
var pass = 'password';
https = require("https"),
https_options = {
"host": "apiserviceprovider",
"path": "/api/login",
"port": 443,
"method": "POST",
"headers": {
"Authorization": createAuth(user, pass)
}
},
request = https.request(https_options, function(response) {
console.log('Request...');
console.log('Status: ' + response.statusCode);
console.log('Headers: ' + JSON.stringify(response.headers));
response.setEncoding('utf8');
response.on('data', function(chunck) {
console.log('BODY: ' + chunk);
});
request.on('end', function() {
console.log('problem with request: ' + chunk);
});
request.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
});