How can I send cookie using NodeJs request GET module?
For anyone stumbling across this in 2019+, the correct way to add a cookie to a request is using the cookie jar property setCookie. request's cookie jar is based on tough-cookie, and you can reference that package's documentation for more detail.
// Require the request
const request = require('request');
// Setup the cookie jar to carry cookies
const cj = request.jar();
// Add your cookie to the jar (URL is parsed into cookie parts)
cj.setCookie(stateCookie, 'https://yoursite.com/');
// Send your request and include the cookie jar
request(
'https://request-site.com/api/things',
{ jar: cj },
(error, response, body)=>{
// do things
}
);
After a few hours I've found a solution, instead of :
//requesting data
request({
url: 'http://localhost/delivery-stats',
method: "GET",
header: {
'set-cookie': cookie
}
it had to be :
//requesting data
request({
url: 'http://localhost/delivery-stats',
method: "GET",
header: {
'Cookie': cookie
}
Because that is correct way to send cookies via request, but it was poorly documented so it took me some time to figure out. Hopefully this would help someone in the future.