question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NodeJS - Cookie Jar Support

See original GitHub issue

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;
});

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:33
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

13reactions
lirantalcommented, 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.

4reactions
andreportelacommented, Jun 7, 2016

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

axios-cookiejar-support - npm
Start using axios-cookiejar-support in your project by running `npm i axios-cookiejar-support`. There are 303 other projects in the npm registry using ...
Read more >
axios-cookiejar-support does not support for use with other ...
My code is const axios = require('axios') const https = require('https'); const axiosCookieJarSupport = require("axios-cookiejar-support"); ...
Read more >
Cookiejar | npm.io
Cookiejar Packages ; tough-cookie. RFC6265 Cookies and Cookie Jar for node.js · cookiecookiesset-cookiecookiejarjarRFC6265RFC2965 ; axios-cookiejar-support. Add ...
Read more >
How to use the axios-cookiejar-support function in ... - Snyk
To help you get started, we've selected a few axios-cookiejar-support examples, based on popular ways it is used in public projects. ; import...
Read more >
request.CookieJar JavaScript and Node.js code examples
Best JavaScript code snippets using request.CookieJar(Showing top 15 results out of 315) · lib/isu/isuJar.js/getJar · test/test-web.js/it · index.js/options. · lib/ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found