Store and forward HTTPOnly cookie

34 views
Skip to first unread message

brenda...@freshenergy.co.za

unread,
May 27, 2016, 9:21:40 AM5/27/16
to nodejs
Hi

I hope someone can help me. I am new to JS and am struggling to store the response httponly cookie from a server so I can send it back in my next request.

I know that httponly doesnt like JS, C# has a cookie container that can be used to store and send the cookies. Can someone point me in the right direction?

I really appreciate the help!!

Denard Springle

unread,
May 28, 2016, 8:11:41 PM5/28/16
to nodejs
httpOnly cookies are *prevented* from being accessed by client-side (e.g. in the browser) JavaScript. This attribute, however, has no effect when making a server-side HTTP request.

There are, effectively, a couple of ways to handle this (assuming, of course, you're making an HTTP request using server-side node)...

1. You can grab the cookie from the response headers themselves and return it with your next request, something along the lines of:

var cookie = get(response.headers, "Set-Cookie")
if (cookie) {
  cookie = (cookie + "").split(";").shift()
  set(opts.headers, "Cookie", cookie)
}

The second approach would be to use Request (https://github.com/request/request) which essentially does the same thing for cookies as the above code, though more elegantly and supports multiple cookies in the request, plus a lot of other things you can't get out of the box with Node and HTTP requests.

HTH

-- Denny
Reply all
Reply to author
Forward
0 new messages