forked from JCMais/node-libcurl
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path05-share.js
76 lines (61 loc) · 2.33 KB
/
05-share.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* 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 one can use the `Share` class to share data between handles
*/
const { Curl, CurlShareLock, Share } = require('../dist')
// first create the share instance that will be used
const share = new Share()
// specify what is going to be shared
share.setOpt('SHARE', CurlShareLock.DataConnect)
share.setOpt('SHARE', CurlShareLock.DataDns)
share.setOpt('SHARE', CurlShareLock.DataCookie)
share.setOpt('SHARE', CurlShareLock.DataSslSession)
// create our handles
const curl1 = new Curl()
const curl2 = new Curl()
// this endpoint will set some Cookies on the first handle
const url1 = 'http://httpbin.org/cookies/set/cookie1name/cookie1value'
// then we are going to call this endpoint with the second one, since the cookies are shared using
// the `Share` instance, it will show the Cookies set on the first request.
const url2 = 'http://httpbin.org/cookies'
curl1.setOpt(Curl.option.URL, url1)
curl1.setOpt(Curl.option.FOLLOWLOCATION, true)
curl1.setOpt(Curl.option.COOKIEFILE, '')
curl1.setOpt(Curl.option.SHARE, share)
curl2.setOpt(Curl.option.URL, url2)
curl2.setOpt(Curl.option.FOLLOWLOCATION, true)
curl2.setOpt(Curl.option.COOKIEFILE, '')
curl2.setOpt(Curl.option.SHARE, share)
curl1.on('end', (statusCode, body, _headers) => {
console.info('Cookies: ', JSON.parse(body))
curl1.close()
console.log()
console.log()
console.log('==================================================')
console.log('================= CURL 2 REQUEST =================')
console.log('==================================================')
curl2.perform()
})
console.log('==================================================')
console.log('================= CURL 1 REQUEST =================')
console.log('==================================================')
curl1.on('error', (error, errorCode) => {
console.error('Error: ', error)
console.error('Code: ', errorCode)
curl1.close()
})
curl2.on('end', (statusCode, body, _headers) => {
console.info('Cookies: ', JSON.parse(body))
curl2.close()
})
curl2.on('error', (error, errorCode) => {
console.error('Error: ', error)
console.error('Code: ', errorCode)
curl2.close()
})
curl1.perform()