user1868405
user1868405

Reputation: 101

In nodejs how to send cookies in a request to a server

how can we receive a cookie as the 'set-cookie' parameter of the response and header and then send this cookie in next request.All this using the 'http' module and no 3rd party modules.I am basically interested on how i will post the cookie as a parameter in the header about to go in the next request

Upvotes: 3

Views: 9577

Answers (2)

user1868405
user1868405

Reputation: 101

oh finally found my mistake i wasnt including the cookie parameter in my header list.I was writing it like this:

var options = {host:url_parsed.host, path:url_parsed.path, method:'GET', 'Cookie':cookie, 'Accept':'/', 'Connection':'keep-alive', };

I should actually be like this:

 var options = {host:url_parsed.host, path:url_parsed.path, method:'GET', headers:{'Cookie':cookie}, 'Accept':'/', 'Connection':'keep-alive', };

Upvotes: 7

prekolna
prekolna

Reputation: 1578

You can write a cookie in the header like this:

response.writeHead(200, {
    'Set-Cookie': 'mycookie=testvalue',
    'Content-Type': 'text/plain'
});
response.end('Hello World\n');

Upvotes: 3

Related Questions