I have tested my other API requests and they all give me a status code, status, and the headers without any problems, However for this particular request its giving me the following error and i have no idea why.
Error which I’m receiving:
Error: read ECONNRESET
at TLSWrap.onStreamRead (internal/stream_base_commons.js:209:20) {
errno: -4077,
code: ECONNRESET,
syscall: 'read'
}
Connect function:
function Connect(server, sId, aId, password) {
console.log("Creating connection")
var postData = {Server: server, SId: sId, AId: aId, Password: password}
var strData = JSON.stringify(postData);
// creating Json web token
const token = jwt.sign({strData}, process.env.TOKEN_SECRET, {algorithm: "HS512"} );
MakeConnectionRequest('//path', strData, token);
}
Below is my client request code:
function MakeConnectionRequest(path, data, token) {
const options =
{
method: 'POST',
protocol: 'https:',
hostname: '', // I do have a hostname - keeping empty
//port: 443,
path: `${path}`,
rejectUnauthorized: false,
headers: {
'Content-Type': 'application/json',
'Content-Length': 89,
'authorisation': `${token}`
},
};
const request = https.request(options, res => {
console.log(`statusCode: ${res.statusCode}`)
res.on('data', d => {
process.stdout.write(d)
})
})
request.on('response', (response) => {
console.log(`STATUS: ${response.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(response.headers)}`);
response.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
})
response.on('end', () => {
console.log('No more data in response.')
});
});
request.on('error', error => {
console.error(error)
})
request.write(data, 'utf-8');
request.end();
}
Is there something I might be doing wrong in my code here? Any advice would be much appreciated!
Thankyou!