protractor node-libcurl Failed: SSL peer certificate or SSH remote key was not OK

2.5k Views Asked by At

I am trying to use node-libcurl module in my protractor project but I am getting the error :

Failed: SSL peer certificate or SSH remote key was not OK

const  {curly} = require('node-libcurl')
const { data } = await curly.post('https://www.example.com', {
  postFields: JSON.stringify({"name":"rak"}),
  httpHeader: [
    'Content-Type: application/json',
    'Accept: application/json',
    'Access-Control-Allow-Origin : *'
  ],
})

How to get rid of this error .

1

There are 1 best solutions below

0
On

From the COMMON_ISSUES.md file on the project's repository:

You need to set either CAINFO or CAPATH options, or disable SSL verification with SSL_VERIFYPEER (not recommended).

The certificate file can be obtained in multiple ways:

  • Extracted directly from your system/browser

  • Downloaded from https://curl.haxx.se/docs/caextract.html, which is based on the one from Firefox

  • Creating a file with the contents of tls.rootCertificates, which was added with Node.js v12.3.0, example:

    const fs = require('fs')
    const path = require('path')
    const tls = require('tls')
    
    const { curly } = require('node-libcurl')
    
    // important steps
    const certFilePath = path.join(__dirname, 'cert.pem')
    const tlsData = tls.rootCertificates.join('\n')
    fs.writeFileSync(certFilePath, tlsData)
    
    async function run() {
      return curly.post('https://httpbin.org/anything', {
        postFields: JSON.stringify({ a: 'b' }),
        httpHeader: ['Content-type: application/json'],
        caInfo: certFilePath,
        verbose: true,
      })
    }
    
    run()
      .then(({ data, statusCode, headers }) =>
        console.log(
          require('util').inspect(
            {
              data: JSON.parse(data),
              statusCode,
              headers,
            },
            null,
            4,
          ),
        ),
      )
      .catch((error) => console.error(`Something went wrong`, { error }))