|
1 | 1 | ## Error: SSL peer certificate or SSH remote key was not Ok
|
2 | 2 |
|
3 |
| -You need to set either [`CAINFO`](https://curl.haxx.se/libcurl/c/CURLOPT_CAINFO.html) or [`CAPATH`](https://curl.haxx.se/libcurl/c/CURLOPT_CAPATH.html) options, or disable SSL verification with [`SSL_VERIFYPEER`](https://curl.haxx.se/libcurl/c/CURLOPT_SSL_VERIFYPEER.html) which is not recommended. |
| 3 | +You need to set either [`CAINFO`](https://curl.haxx.se/libcurl/c/CURLOPT_CAINFO.html) or [`CAPATH`](https://curl.haxx.se/libcurl/c/CURLOPT_CAPATH.html) options, or disable SSL verification with [`SSL_VERIFYPEER`](https://curl.haxx.se/libcurl/c/CURLOPT_SSL_VERIFYPEER.html) (not recommended). |
4 | 4 |
|
5 | 5 | The certificate file can be obtained in multiple ways:
|
6 |
| -1. Downloaded from https://curl.haxx.se/docs/caextract.html |
7 |
| -2. Creating a file with the contents of `tls.rootCertificates`, which was added with Node.js `v12.3.0` |
| 6 | + |
| 7 | +1. Extracted directly from your system/browser |
| 8 | +2. Downloaded from https://curl.haxx.se/docs/caextract.html, which is based on the one from Firefox |
| 9 | +3. Creating a file with the contents of `tls.rootCertificates`, which was added with Node.js `v12.3.0`, example: |
| 10 | +```javascript |
| 11 | +const fs = require('fs') |
| 12 | +const path = require('path') |
| 13 | +const tls = require('tls') |
| 14 | + |
| 15 | +const { curly } = require('node-libcurl') |
| 16 | + |
| 17 | +// important steps |
| 18 | +const certFilePath = path.join(__dirname, 'cert.pem') |
| 19 | +const tlsData = tls.rootCertificates.join('\n') |
| 20 | +fs.writeFileSync(certFilePath, tlsData) |
| 21 | + |
| 22 | +async function run() { |
| 23 | + return curly.post('https://httpbin.org/anything', { |
| 24 | + postFields: JSON.stringify({ a: 'b' }), |
| 25 | + httpHeader: ['Content-type: application/json'], |
| 26 | + caInfo: certFilePath, |
| 27 | + verbose: true, |
| 28 | + }) |
| 29 | +} |
| 30 | + |
| 31 | +run() |
| 32 | + .then(({ data, statusCode, headers }) => |
| 33 | + console.log( |
| 34 | + require('util').inspect( |
| 35 | + { |
| 36 | + data: JSON.parse(data), |
| 37 | + statusCode, |
| 38 | + headers, |
| 39 | + }, |
| 40 | + null, |
| 41 | + 4, |
| 42 | + ), |
| 43 | + ), |
| 44 | + ) |
| 45 | + .catch((error) => console.error(`Something went wrong`, { error })) |
| 46 | +``` |
0 commit comments