Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get cookies from request module in node.js?

Tags:

function getCookies(){      request('http://google.com', function (error, response, body) {         if (!error && response.statusCode == 200) {             console.log(response.headers);         }     }) } 

Result

{ date: 'Fri, 11 Dec 2015 07:15:50 GMT',   expires: '-1',   'cache-control': 'private, max-age=0',   'content-type': 'text/html; charset=EUC-KR',   p3p: 'CP="This is not a P3P policy! See https://www.google.com/support/accounts/answer/151657?hl=en for more info."',   server: 'gws',   'x-xss-protection': '1; mode=block',   'x-frame-options': 'SAMEORIGIN',   'set-cookie':     [ 'PREF=ID=1111111111111111:FF=0:TM=1449818150:LM=1449818150:V=1:S=Q3BB20FA6TkaZymd; expires=Thu, 31-Dec-2015 16:02:17 GMT; path=/; domain=.google.co.kr',      'NID=74=hnriWxk7N9jHtP5W0qgaxrwD1YuNKOmJg748ucxWilu9jaqHJVovfkYdvMr0tlp-VToID5cdTNDSXNXqr4M8umJ9traab67x2xZKfu3hJbsBRXeVvyiCOcwZ8bkXNcU4; expires=Sat, 11-Jun-2016 07:15:50 GMT; path=/; domain=.google.co.kr; HttpOnly' ],   'accept-ranges': 'none',   vary: 'Accept-Encoding',   connection: 'close' } 

I want to pick up value of 'set-cookie' from response headers. How to pick it up? Is there any cool and simple way? Should I use for statement from filedkey, or. What should I do? I don't know I'm totally newbie on Javascript. Thanks...

like image 361
ton1 Avatar asked Dec 11 '15 07:12

ton1


People also ask

How do I request a cookie in node js?

createServer(function (request, response) { // To Read a Cookie const cookies = parseCookies(request); // To Write a Cookie response. writeHead(200, { "Set-Cookie": `mycookie=test`, "Content-Type": `text/plain` }); response. end(`Hello World\n`); }). listen(8124); const {address, port} = server.

What is the difference between request cookies and response cookies?

Response cookies are cookies that you want the browser to set on the client machine. Request cookies are cookies that already exist on the client side and have been sent by the browser together with the request. In comparison there is only one Session State. and it lives on the server.

What is the use of Request module in node js?

The request module is used to make HTTP calls. It is the simplest way of making HTTP calls in node. js using this request module. It follows redirects by default.

Is request module deprecated?

As of February 11, 2020, one of the biggest NPM packages — Request — has been officially deprecated. This popular library has been around for more than a decade, with the first version released in 2009. Since then, it has received more than 16 million weekly downloads and more than 47,000 libraries are dependent on it.


1 Answers

function getCookies(callback){      request('http://google.com', function (error, response, body) {         if (!error && response.statusCode == 200) {             return callback(null, response.headers['set-cookie']);         } else {             return callback(error);         }     }) } 

then you can call:

getCookies(function(err, res){     if(!err)        console.log(res) }) 
like image 94
BlackMamba Avatar answered Sep 20 '22 22:09

BlackMamba