Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NodeJS - Cookie Jar Support #48

Closed
nicholasrobinson opened this issue Mar 11, 2015 · 9 comments
Closed

NodeJS - Cookie Jar Support #48

nicholasrobinson opened this issue Mar 11, 2015 · 9 comments
Milestone

Comments

@nicholasrobinson
Copy link

axios running on NodeJS does not provide Cookie jar support natively. Ideally the library would either provide built in cookie jar functionality or easily accommodate existing cookie jar libraries.

The Request API could be modified to support the following options:

{
...
  // `jar` is a boolean that controls cookie jar support for the request
  jar: true, // default
...
}

or

{
...
  // `jar` is a cookie jar that will be used for the request
  jar: cookieJar, // default
...
}

where

var tough = require('tough-cookie');
var cookiejar = new tough.CookieJar();

Note:
Trivial cookie support using tough-cookie can be added by leveraging interceptors:

var tough = require('tough-cookie');
var Cookie = tough.Cookie;
var cookiejar = new tough.CookieJar();

axios.interceptors.request.use(function (config) {
  cookiejar.getCookies(config.url, function(err, cookies) {
    config.headers.cookie = cookies.join('; ');
  });
  return config;
});

axios.interceptors.response.use(function (response) {
  if (response.headers['set-cookie'] instanceof Array) {
    cookies = response.headers['set-cookie'].forEach(function (c) {
      cookiejar.setCookie(Cookie.parse(c), response.config.url, function(err, cookie){});
    });
  }
  return response;
});
@mzabriskie
Copy link
Member

@nicholasrobinson you say that using interceptors provides "trivial cookie support". What is missing for full support? Also, is there a case where you wouldn't want cookie support for node? Specifically, does there need to be an option, or just always manage cookies for requests with node?

@nicholasrobinson
Copy link
Author

I would imagine that "full" cookie jar support would:

  • respect cookie deletion headers in server response
  • respect cookie expiration
  • maybe more..?

As for cases where you wouldn't want cookie support:

  • Unit testing of http endpoints?
  • Deliberately ignoring cookie headers?

IMO there should be a flag to disable cookie jar support. More generally speaking though it is useful to be able to emulate browser functionality in automated testing scenarios.

@mzabriskie mzabriskie added this to the 0.7.0 milestone Mar 19, 2015
@mzabriskie mzabriskie mentioned this issue Jan 22, 2016
8 tasks
@damoclark
Copy link

In Node, you may also wish to use different cookie jars depending on which sites you are accessing. A default cookie jar could be defined, but an alternate used on specific requests. Just leverage the existing merging of options that Axios already provides.

@andreportela
Copy link

I was recently trying to do just that (add trivial cookie support) and I've used the code sample posted by @nicholasrobinson. Unfortunately I couldn't make it work like that.
It seems that axios.interceptors.request is triggered between the request being made and then callback being executed. So it doesn't inject the cookie in the request.

I made it work by manually capturing the cookie on the first server response and then manually injecting the cookie into the following requests (which is ugly) like this

function getCookie(response) {
    return response.headers['set-cookie'];
}

function makeRequestWithGivenCookie(cookie) {
    return axios.get('/',{headers: {"Cookie": cookie}});
}

Am I missing something here? BTW axios is great! Cookie support would really be an awesome feature.

@rubennorte
Copy link
Member

rubennorte commented Jul 19, 2016

Please, take a look at #206 (comment)

var axios = require('axios');
var axiosCookieJar = require('axios-cookiejar'); // It does not exist yet

axiosCookieJar(axios, options);

// Or
var instance = axios.create(...);
axiosCookieJar(instance, options);

@damoclark, you could do that easily with the kind of plugin that I described, using different axios instances for those specific requests you talked about.

@lirantal
Copy link

lirantal commented Jan 1, 2017

would definitely like to see cookies/persistent sessions support in axios. A third party module is a great addition but there's no guarantee on it's maintainability in the future.

@rubennorte
Copy link
Member

@lirantal we're rather short maintaining the functionality Axios has at the moment so adding things that can be implemented in plugins won't help that.

I'm closing this issue as it won't be implemented in core and there's a library that implements this in ECOSYSTEM.md

@nzvtrk
Copy link

nzvtrk commented Apr 5, 2019

@andreportela
I'm writed simple example for works with cookie/session in node.js

https://gist.github.com/nzvtrk/ebf494441e36200312faf82ce89de9f2

@andreportela
Copy link

Hi @nzvtrk I didn't have the time to take a look at it. But thanks for sharing! 😃 It may be useful to a lot of people that end up here.

It has been a long time since I moved on. But it seems that this feature is already implemented on a lib. Maybe I would rather just use it. 🤷‍♂️

@axios axios locked and limited conversation to collaborators May 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants