fromURL with cookies

544 views
Skip to first unread message

Jasper Chow

unread,
Apr 18, 2018, 1:53:12 PM4/18/18
to jsdom
Hello. I am new to jsdom

My questionis What is the best practice to set cookie if using  JSDOM(html, options), or JSDOM.fromURL(url, options) API ?  

Assume I have the following working
JSDOM.fromURL(`http://www.node.org/`, options).then(dom => {console.log(dom.serialize());})

Now if I change URL to something that requires authentication. I get a 401 error which ... makes sense. 
What is the recommended approach to set the cookie, along with fromURL?

var cookiejar = new tough.CookieJar()     or ??? var cookiejar = jsdom.createCookieJar();
var mycookie = {"key1=value1;key2=value2;...."};

let options={ runScripts: "dangerously",
                     resources: "usable", 
     cookieJar: cookiejar,
                     cookie: mycookie
                    };


I don't think either approach works. Is tough cookies proper way with latest jsdom?  and should setting the cookie in options work? Any suggestion is appreciated. Thanks
Jasper

Domenic Denicola

unread,
Apr 19, 2018, 1:44:17 AM4/19/18
to js...@googlegroups.com

See the readme documentation: https://github.com/jsdom/jsdom#cookie-jars

 

(The same option also works for fromURL.)

--
You received this message because you are subscribed to the Google Groups "jsdom" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jsdom+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages