forked from JCMais/node-libcurl
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path09-cookies.js
41 lines (31 loc) · 1.08 KB
/
09-cookies.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 one can receive cookies and store them in a file.
*/
const path = require('path')
const fs = require('fs')
const { Curl } = require('../dist')
const curl = new Curl()
// this endpoint will set a cookie named `cookie1name` with value `cookie1value`
const url = 'http://httpbin.org/cookies/set/cookie1name/cookie1value'
const cookieJarFile = path.join(__dirname, 'cookiejar.txt')
curl.setOpt(Curl.option.URL, url)
curl.setOpt(Curl.option.VERBOSE, true)
curl.setOpt(Curl.option.FOLLOWLOCATION, true)
curl.setOpt(Curl.option.COOKIEFILE, cookieJarFile)
curl.setOpt(Curl.option.COOKIEJAR, cookieJarFile)
if (!fs.existsSync(cookieJarFile)) {
fs.writeFileSync(cookieJarFile)
}
curl.perform()
curl.on('end', () => {
curl.close()
console.info('Cookie file contents:')
console.info(fs.readFileSync(cookieJarFile).toString('utf8'))
})
curl.on('error', curl.close.bind(curl))