Marco Grieco
Marco Grieco

Reputation: 229

NodeJS + Got - Share Session Cookies

i need to invoke two different APIs: the first one is used to login, the second one to extract data.

The first login stores Cookies that must be used in the second request.

In PHP i have implemented this process as follow

$login_url = 'EXTERNALWEBSITE/login';

//These are the post data username and password
$post_data = 'email=EMAIL&pwd=PWD';

//Create a curl object
$ch = curl_init();

//Set the useragent
$agent = $_SERVER["HTTP_USER_AGENT"];   
curl_setopt($ch, CURLOPT_USERAGENT, $agent);

//Set the URL
curl_setopt($ch, CURLOPT_URL, $login_url );

//This is a POST query
curl_setopt($ch, CURLOPT_POST, 1 );

//Set the post data
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

//We want the content after the query
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);


//Follow Location redirects
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

/*
Set the cookie storing files
Cookie files are necessary since we are logging and session data needs to be saved
*/

curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');

//Execute the action to login
$postResult = curl_exec($ch);

$url = 'EXTERNALWEBSITE/secondapi'; 

curl_setopt_array(
$ch, array(
CURLOPT_URL => $url ,
CURLOPT_RETURNTRANSFER => true
));

$output = curl_exec($ch);
    
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch).'<br/>';        
    $output = null;
} 

curl_close ($ch);

return $output;

Which is the way in NodeJS using GOT Library or similar? I have tried a lot of suggestion without success. Here my not working solution

login().then(res => {

        getData(res.headers['set-cookie'][0]);
        
    })
    .catch(err => {
        console.log('Error: ', err.message);
    }); 

  const login = () => {

const options = {
    prefixUrl: constants.BASEURL,
    json: {
        email: constants.USER,
        pwd: constants.PASSWORD
    }
};

return got.post(constants.LOGINURL, options);

}   

const getData= async (cookie) => {


const options = {
    prefixUrl: constants.BASEURL,
    headers: {
        Cookie: cookie
    }
};


const res = await got.post(constants.DATAURL, options);


}

Thanks you!

Upvotes: 0

Views: 1941

Answers (1)

Marco Grieco
Marco Grieco

Reputation: 229

I have solved using the library node-libcurl. Here the code

const cookie = 'cookie.txt';

var { statusCode, data, headers } = await curly.post(constants.BASEURL+constants.LOGINURL, {
    postFields: querystring.stringify({
        pwd: constants.PASSWORD,
        email: constants.AUSER
    }),
    followLocation: true,
    cookieFile: cookie,
    cookieJar: cookie,
    sslVerifyPeer: false,
    post: true
});

var { statusCode, data, headers } = await curly.get(constants.BASEURL+constants.DATAIURL, {
    followLocation: true,
    cookieFile: cookie,
    cookieJar: cookie,
    sslVerifyPeer: false
});


return data;

Declaring the cookie const on top, the cookieJar is shared between the two calls, so you can simulate a session managed by cookies.

Thank you!

Upvotes: 1

Related Questions