0

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();

1 Answer 1

1

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:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.