Can I send a GET request with cookies in the headers in Node?

In the browser, if I send a GET request, the request will send a cookie in the meantime. Now I want to simulate a GET request from Node, and then how to write code?

+5
source share
3 answers

The use of wonderful library request cookies is enabled by default. You can send your own like this (taken from the Github page):

var j = request.jar()
var cookie = request.cookie('your_cookie_here')
j.add(cookie)
request({url: 'http://www.google.com', jar: j}, function () {
  request('http://images.google.com')
})
+13
source

http:request(), Set-Cookie (. HTTP , ) headers options ; cookie. Mikeal request cookieParser connect, .

Femi : , , , , . , , , , , , , .

+4
var jar = request.jar();
const jwtSecret = fs.readFileSync(`${__dirname}/.ssh/id_rsa`, 'utf8');
const token = jwt.sign(jwtPayload, jwtSecret, settings);
jar.setCookie(`any-name=${token}`, 'http://localhost:12345/');
const options = {
  method: 'GET',
  url: 'http://localhost:12345',
  jar,
  json: true
};
request(options, handleResponse);
0

All Articles