I've written an authentication request that works with the request module but fails to return the desired 'authentication token' cookie with the node-fetch module that I'm trying to move towards since 'request' has been deprecated.Here is the working code using 'request'var callback1 = function(err, httpResponse, body){console.log("Correctly prints all the cookies we want: ");console.log(httpResponse.headers["set-cookie"]);if (err){console.log("here it is!"); throw err;}else {//do more with response}};var callback0 = function(err, httpResponse0, body){console.log("Check that sent cookies are identical, from request:");console.log(httpResponse0.headers["set-cookie"][0].substr(0,httpResponse0.headers["set-cookie"][0].indexOf(";")));if (err){throw err;}else {var options1 = {url: urlString1,headers:{'Host': hostName,'User-Agent': myUserAgent,'Accept': myAccept,'Accept-Language':myAcceptLanguage,'Accept-Encoding': myAcceptEncoding,'Referer': "https://"+hostName+'/login','Cookie': httpResponse0.headers["set-cookie"][0].substr(0,httpResponse0.headers["set-cookie"][0].indexOf(";")),'Content-Type': "application/x-www-form-urlencoded",'Content-Length': Buffer.byteLength(querystring.stringify(postData)),'Connection': "keep-alive",'Upgrade-Insecure-Requests': "1",'DNT': "1",'TE': "Trailers"},form: {email: myEmail, password: myPassword}};request.post(options1, callback1);}}var options0 = {url: urlString0,headers:{'Host': hostName,'User-Agent': myUserAgent,'Accept': myAccept,'Accept-Language':myAcceptLanguage,'Accept-Encoding': myAcceptEncoding,'Referer': "https://"+hostName,'Connection': "keep-alive",'Upgrade-Insecure-Requests': "1"}};request(options0, callback0);And here is the code in node-fetch that's failing to properly return the auth_token cookie:const fetchOptions0 = {method: 'GET',headers:{'Host': hostName,'User-Agent': myUserAgent,'Accept': myAccept,'Accept-Language':myAcceptLanguage,'Accept-Encoding': myAcceptEncoding,'Referer': "https://"+hostName,'Connection': "keep-alive",'Upgrade-Insecure-Requests': "1"}}fetch(urlString0, fetchOptions0).then(res0 => {console.log("Check that sent cookies are identical, from fetch:");console.log(res0.headers.raw()['set-cookie'][0].substr(0, res0.headers.raw()['set-cookie'][0].indexOf(";")));const FormData = require('form-data');const myForm = new FormData();myForm.append('email', myEmail);myForm.append('password', myPassword);var fetchOptions1 = {method: 'POST',headers:{'Host': hostName,'User-Agent': myUserAgent,'Accept': myAccept,'Accept-Language':myAcceptLanguage,'Accept-Encoding': myAcceptEncoding,'Referer': "https://"+hostName+'/login','Cookie': res0.headers.raw()['set-cookie'][0].substr(0, res0.headers.raw()['set-cookie'][0].indexOf(";")),'Content-Type': "application/x-www-form-urlencoded",'Content-Length': Buffer.byteLength(querystring.stringify(postData)),'Connection': "keep-alive",'Upgrade-Insecure-Requests': "1",'DNT': "1",'TE': "Trailers"},body:myForm};fetch(urlString1, fetchOptions1).then(res1=> {console.log("Incorrect cookie, missing auth:"); console.log(res1.headers.raw()['set-cookie']);});});I have tried writing the form data using JSON.stringify as suggested in this answer, but that didn't help
Submitted February 20, 2020 at 05:33AM by garson515
No comments:
Post a Comment