DEV Community

Flaviu Tamasan
Flaviu Tamasan

Posted on

Node JS POST Request including certificates and proxy configuration

i have a special issue that i cannot fix. My setup is as follows:
i want to make a POST request from behind a proxy using also certificates. I tried to use node-fetch or axios for this but axios has a defect when using a POST request and proxys and in node-fetch you only can use a proxy or the certificate as an agent, not both. Now i tried to use the node build in https module and wrote the following code (data is dummy):

const body = {}

const data = JSON.stringify(body)

const options = {
    host: proxy.host,
    port: proxy.port,
    path: 'https://server:port/path',
    method: 'POST',
    headers: {
        'Proxy-Authorization': auth,
        'Accept': 'application/json',
        'content-type': 'application/json',
        'requestid': 'ec69aa5d-52d8-4849-8b95-6e360f472860',
        'testid': '9b8183ed-967c-4701-bfa4-dd8c0ec6bab1',
    },
    key: fs.readFileSync('certificates/client.key.pem'),
    cert: fs.readFileSync('certificates/client.cert.pem'),
    agent: false
}

const req = https.request(options, (res) => {
    console.log('statusCode:', res.statusCode);
    console.log('headers:', res.headers);

    res.on('data', (d) => {
        process.stdout.write(d);
    });
});

req.write(data, () => {
    console.log(req)
});
req.on('error', (e) => {
    console.error(e);
});

req.end();
Enter fullscreen mode Exit fullscreen mode

but this also does not work. It seams to me that the certificates are used for the proxy not for the actual URL in this case as i get the following error:

Error: write EPROTO 4348: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:92:16) {
  errno: 'EPROTO',
  code: 'EPROTO',
  syscall: 'write'
}
Enter fullscreen mode Exit fullscreen mode

triggering the request with CURL works with no problems.

can you assist here somehow? Perhaps suggest another library that i can use for making this complex request?

thanks,
Flaviu

Top comments (0)