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?
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') })
http:request(), Set-Cookie (. HTTP , ) headers options ; cookie. Mikeal request cookieParser connect, .
http:request()
Set-Cookie
headers
options
request
cookieParser
connect
Femi : , , , , . , , , , , , , .
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);