NodeJS - Cookie Jar Support
See original GitHub issueaxios 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:
- Created 9 years ago
- Reactions:33
- Comments:9 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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 andthen
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
Am I missing something here? BTW axios is great! Cookie support would really be an awesome feature.