Rodolfo Oliveira
Rodolfo Oliveira

Reputation: 235

read ECONNRESET error in nodejs

I extracted this piece of code from nodejs documentation Link, but when i run using my nodejs server It gave me this error. Some people in stack overflow had the same error, but I didn't find solution.

  var postData = querystring.stringify({
    'msg' : 'Hello World!'
  });

  var options = {
    host: 'westus.api.cognitive.microsoft.com',
    port: 443,
    path: '/vision/v1.0/ocr?language=pt&detectOrientation=true',
    method: 'POST',
    headers: {'Ocp-Apim-Subscription-Key': 'API_KEY',
      'Content-type': 'application/json'
    }
  };
  var req = http.request(options, (res) => {
    console.log(`STATUS: ${res.statusCode}`);
    console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
    res.setEncoding('utf8');
    res.on('data', (chunk) => {
      console.log(`BODY: ${chunk}`);
    });
    res.on('end', () => {
      console.log('No more data in response.');
    });
  });

  req.on('error', (e) => {
    console.log(`problem with request: ${e.message}`);
  });

  // write data to request body
  req.write(postData);
  req.end();

Upvotes: 0

Views: 1986

Answers (1)

user2065736
user2065736

Reputation:

You seem to have used http instead of https. The request you have initiated is meant for and HTTPS connection. The TLS handshake fails since http module is incapable of understanding the underlying TLS protocol.

Replace http with https and you'll be fine.

More in details:

Upvotes: 1

Related Questions