mikeal/request — Issue #125: headers.cookie shouldn't be replaced
Author: papandreou
Created: 2011-12-05 13:41:58+00:00
State: open

I had some old code that pre-dates request's cookie jar support:

request({
headers: {
cookie: "foo=bar"
},
// ...
});

However, after I updated request it stopped working because now `headers.cookie` is always overwritten unless `jar: false` is specified:

if (self.jar === false) {
// disable cookies
var cookies = false;
self._disableCookies = true;
} else if (self.jar) {
// fetch cookie from the user defined cookie jar
var cookies = self.jar.get({ url: self.uri.href })
} else {
// fetch cookie from the global cookie jar
var cookies = cookieJar.get({ url: self.uri.href })
}
if (cookies) {
var cookieString = cookies.map(function (c) {
return c.name + "=" + c.value;
}).join("; ");

self.headers.Cookie = cookieString;
}

The fix was trivial, but I'm reporting it anyway because I think it's a wtf. In my opinion there should be a check for whether `self.headers.cookie` already exists, and if so, append to it. Or at least it shouldn't do anything if the global cookie jar is empty.

My guess is that the current behavior breaks a bunch of proxying use cases where cookies are involved.

I can whip up a pull request, but it would be nice to hear if everyone else is on board with this?