简体   繁体   中英

How to use the requestJS get method to set the cookie?

    let headers = {
    'Accept': xxxxx,
    'Host': 'xxxx',
    'Referer': 'https://xxxx/',
  }
  request.get(url, {
    json: true,
    headers: headers
  }).on('response', function (response) {
    let headers = response.headers['set-cookie'];
  })

If I use

headers = {
 'Accept': xxx,
 'cookie':xxx
}

it will memory leak

According to the README of request , you can use request.jar() to set your own cookie to the request.

var j = request.jar();
var cookie = request.cookie('key1=value1');
var url = 'http://www.google.com';
j.setCookie(cookie, url);
request({url: url, jar: j}, function () {
  request('http://images.google.com')
})

UPDATE:

They said,

Cookies are disabled by default (else, they would be used in subsequent requests). To enable cookies, set jar to true (either in defaults or options).

So you can enable request to use cookies just to set { jar: true } .

var request = request.defaults({jar: true})
request('http://www.google.com', function () {
  request('http://images.google.com')
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM