how to using cookie with request (request , tough-cookie , node.js)

10,504

I just found the solution ....

theJar.setCookie('test=1234; path=/; domain=examples.com', 'http://examples.com/');

hm...I have to say, the document which for request is not so good..., lol

Share:
10,504
PatrickSCLin
Author by

PatrickSCLin

Updated on June 14, 2022

Comments

  • PatrickSCLin
    PatrickSCLin about 2 years

    I'm wondering to know how to using cookie with request (https://github.com/mikeal/request)

    I need to set a cookie which able to be fetched for every sub domains from request,

    something like

    *.examples.com

    and the path is for every page, something like

    /

    then server-side able to fetch the data from cookie correctly, something like

    test=1234

    I found the cookies which setup from response was working fine,

    I added a custom jar to save the cookies, something like

    var theJar = request.jar();
    
    var theRequest = request.defaults({
                headers: { 
                    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36' 
                } 
              , jar: theJar
            });
    

    but the cookies which I setup from request, only able to be fetched in same domain,

    and I can't find a method to setup cookie in more options

    for now if I want one cookie which able to be fetched in three sub domains,

    I have to setup like this way:

    theJar.setCookie('test=1234', 'http://www.examples.com/', {"ignoreError":true});
    
    theJar.setCookie('test=1234', 'http://member.examples.com/', {"ignoreError":true});
    
    theJar.setCookie('test=1234', 'http://api.examples.com/', {"ignoreError":true});
    

    Is here any advance ways to setup a cookie from request,

    made it able to be fetched in every sub domains ???

  • Nicolas S.Xu
    Nicolas S.Xu almost 10 years
    I am making http request to localhost, but cookie is not carried along requests. how do I make sure cookie is carried in the header using request with jar: true?
  • Michael
    Michael over 7 years
    setCookie seems really wonky... it doesn't autodetect "secure", it doesn't set the domain from the URL i passed, and using "ignoreError" results in the callback never being called.