forked from JCMais/node-libcurl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path14-ssl.js
41 lines (33 loc) · 1.22 KB
/
14-ssl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* Copyright (c) Jonathan Cardoso Machado. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* Example showing how to make a request for an endpoint that uses SSL
*/
const path = require('path')
const { Curl } = require('../dist')
const url = 'https://www.google.com'
const certfile = path.join(__dirname, 'cacert.pem')
const curl = new Curl()
curl.setOpt('URL', url)
curl.setOpt('FOLLOWLOCATION', 1)
curl.setOpt('VERBOSE', 1)
// Generally libcurl knows where to find the CA Cert of the system
// in case that does not work, you need to specify it yourself, otherwise
// you are going to receive the error 'Peer certificate cannot be authenticated with given CA certificates'
// More info on http://curl.haxx.se/docs/sslcerts.html and http://curl.haxx.se/docs/caextract.html
if (certfile) {
curl.setOpt('CAINFO', certfile)
//This is not a boolean field! 0 -> Disabled, 2 -> Enabled
curl.setOpt('SSL_VERIFYHOST', 2)
curl.setOpt('SSL_VERIFYPEER', 1)
} else {
curl.setOpt('SSL_VERIFYHOST', 0)
curl.setOpt('SSL_VERIFYPEER', 0)
}
curl.on('end', curl.close.bind(curl))
curl.on('error', curl.close.bind(curl))
curl.perform()