Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Handle cookies #82

Closed
juggy opened this Issue · 15 comments

7 participants

@juggy

I stumbled upon a page that set cookies in the redirection. I don't know if it is part of the http spec or not, but I had to change the redirection code within request to handle this.

Since request does not handle cookies itself, it might make sense to build a generic callback where the caller can play with the headers etc.

Thanks,
Julien.

Mikeal changed the title to add more generic support for cookies.

@mikeal
Owner
@juggy

What libraries did you look at?

@Pita

+1

@waynelee

juggy -- care to share your cookie changes to request? i'll take hacks!

+1 on callback support to twiddle with headers or cookies support.

@waynelee

Here's a patch that adds cookie support. Warning, js and node.js and git and github and http newbie here. It works for my application, but no idea if it is per spec. Depends on node-cookiejar, which means package.json should be updated.

From 4bf36075d354dd5c41f1598249bf05a161da0cc2 Mon Sep 17 00:00:00 2001
From: Wayne Lee waynelee@wal-imac.local
Date: Tue, 11 Oct 2011 23:27:44 -0700
Subject: [PATCH] Add cookie support in redirects


main.js | 8 ++++++++
1 files changed, 8 insertions(+), 0 deletions(-)

diff --git a/main.js b/main.js
index de75caf..28ef534 100644
--- a/main.js
+++ b/main.js
@@ -263,6 +263,14 @@ Request.prototype.request = function () {
self.uri = response.headers.location
self.redirects.push( { statusCode : response.statusCode,
redirectUri: response.headers.location })
+

  • if (self.response.headers['set-cookie']) {
  • var cj = require('cookiejar').CookieJar()
  • cj.setCookies(response.headers['set-cookie'])
  • cookies = cj.getCookies(CookieAccessInfo(url.parse(self.uri).host))
  • self.headers['Cookie'] = cookies.toValueString()
  • } + delete self.req delete self.agent delete self._started -- 1.7.4.4
@juggy

@waynelee Good but you might do the initial request with cookies, use those cookies for the redirection and, finally, se them after the redirection. My code is similar but you have a callback when the redirection occurs where you can do whatever you want (including cookies).

@mikeal
Owner

this is the API i want for cookies

var j = request.jar()
r({url:'http://www.google.com', jar:j}, function () {
  r({url:'http://images.google.com', jar:j})
})

The jar holds the state of the cookies being set, each request sends the proper cookies for that domain. The boiler is easy to get rid of with defaults.

var j = request.jar()
var r = request.defaults({jar:j})
request('http://www.google.com', function () {
  request('http://images.google.com')
})
@Marak

It looks like https://github.com/bmeck/node-cookiejar might be a good candidate for this API?

@alessioalex - Do you want to try looking into this and seeing what you can do?

@mikeal
Owner

if we go with node-cookie jar please check the dependency. i do not want to add dependencies to the package.json if at all avoidable.

@juggy

I think you can solve this without actually adding any specific code for cookies. When you do your request/redirect, a callback can be called to let the user deal with the headers/cookies. No new dependencies would be needed and it would be a little more future proof.

As a user of request, if I need cookies, I use any of the available libs to do that (or deal with them myself). A wiki page or a code exemple should be available to ease the implementation.

What do you think?

@Pita

@juggy I did this, its a pain

@alessioalex

I have this almost 90% implemented, no dependencies needed, I'll finish it in one hour or so and then make a pull request. Bear with me :)

@lbdremy

What's up about this issue, guys? I have the same problem.

@mikeal
Owner

this got merged forever ago.

@mikeal mikeal closed this
@cybdoom cybdoom referenced this issue from a commit in cybdoom/EbayAmazonApiServer
@cybdoom cybdoom