Error "ssl3_get_record:wrong version number" in node.js when making https post request

Viewed 26232

I'm trying to make a https request to an api of my work using node.js through the following script

const https = require('https');
const options = {
    hostname,
    path: fullPath,
    port: 80,
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Content-Length': data.length
    }
};
const req = https.request(options, (res) => {
    console.log('statusCode:', res.statusCode);
    res.on('data', chunk => console.log('chunk:', chunk));
});
req.on('error', error => {
    console.log('Failed!');
    console.log(error);
});
req.write(data);
req.end();

And as a response i get

Failed!
Error: write EPROTO 6772:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:c:\ws\deps\openssl\openssl\ssl\record\ssl3_record.c:332:

    at WriteWrap.onWriteComplete [as oncomplete] (internal/stream_base_commons.js:87:16) {
  errno: 'EPROTO',
  code: 'EPROTO',
  syscall: 'write'
}

I made this request on a browser using jQuery and it worked

$.post({
    url, data,
    success: res => console.log(res),
});

I tryed to use the header X-SSL-PROTOCOL with no success. The url and data are hidden because they are confidential, but they are the same in the jQuery example and in node.js.

1 Answers
    port: 80,
    method: 'POST',
    ...
const req = https.request(options, (res) => {

You are using HTTPS against port 80, which is usually plain HTTP. The response you get suggest that the server is not providing HTTPS on this port, which is actually the expected behavior. HTTPS is instead usually done on port 443.

I made this request on a browser using jQuery and it worked

It is unclear what url is in this example but if it just was https://domain/path then port 443 was used. Port 80 would have only been used with HTTPS if explicitly given, i.e. https://domain:80/path.

Related