Giter VIP home page Giter VIP logo

Comments (9)

lirantal avatar lirantal commented on March 29, 2024 13

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.

from axios.

nicholasrobinson avatar nicholasrobinson commented on March 29, 2024 4

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.

from axios.

andreportela avatar andreportela commented on March 29, 2024 4

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.

from axios.

mzabriskie avatar mzabriskie commented on March 29, 2024 1

@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?

from axios.

damoclark avatar damoclark commented on March 29, 2024 1

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.

from axios.

rubennorte avatar rubennorte commented on March 29, 2024 1

@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

from axios.

rubennorte avatar rubennorte commented on March 29, 2024

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.

from axios.

nzvtrk avatar nzvtrk commented on March 29, 2024

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

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

from axios.

andreportela avatar andreportela commented on March 29, 2024

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. 🤷‍♂️

from axios.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.