I’am trying to call this API
https://cwe-api.mitre.org/api/v1/cwe/weakness/all
from node.js server, this suppose to get data of 8MB, However I get this error
Error: read ECONNRESET
at TLSWrap.onStreamRead (node:internal/stream_base_commons:217:20) { errno: -4077, code: ‘ECONNRESET’, syscall: ‘read’ }
I have tried to make timeout 5 min and using Axios but nothing seems to work, However when I use postman to send this Requests it fetch the request in about 2.5 sec without any problem.
const sendHttpRequest = async (method, url, timeout = null) => {
const https = require('https');
return new Promise((resolve, reject) => {
const agent = new https.Agent({
keepAlive: true,
maxSockets: 100
});
const options = {
method: method,
timeout: timeout,
agent: agent
};
const req = https.request(url, options, (res) => {
let data = '';
// Handle incoming data
res.on('data', (chunk) => {
data += chunk;
});
// Resolve the promise with the full data once the response ends
res.on('end', () => {
resolve(data);
});
});
req.on('error', (error) => {
reject(error);
});
req.on('timeout', () => {
reject(new Error('Request timed out'));
});
req.end();
});
Using Service
const cweThreatArrayDbURL = 'https://cwe-api.mitre.org/api/v1/cwe/weakness/all';
let apiData = sendHttpRequest("GET", cweThreatArrayDbURL, (30*1000))
.then((data)=> console.log('response', data))
.catch(err => console.log(err));