How to fetch Cookies from one REST API and applied on other REST API?

KausikSharma
Tera Contributor

KausikSharma_0-1693826140175.png

Here the intension is , from get csrf  API we have to fetch X-CSRF-TOKEN and cookies. X-CSRF-TOKEN able to fetch and applied but how to fetch the whole cookies and applied on the next create charm ID?

 

3 REPLIES 3

Craig Gruwell
Mega Sage

I'm not familiar with that integration but what do you get if you add this code before your second API call?

 

 

gs.print(responseCsrf.getCookies());

 

 

As far as X-CSRF-TOKEN, does the documentation state if that is returned as part of the response body?  You can add this to check?

 

gs.print(responseCsrf.getBody());

 

 

 

durga017
Tera Expert

Hi @KausikSharma 
Certainly, you can enhance the code by separating the two REST API functions and passing cookies as parameters. Here's a revised version:

 

(function executeRule(current, previous /null when async/ ) {
var reqcsrf = new sn_ws.RESTMessageV2("SAP-SOLMAN Integration API', 'Get CSRF"); reqCsrf.setRequestHeader("x-csrf-token", "fetch");
var responseCsrf reqCsrf.execute();
var csrflody = responseCsrf.getBody();
var csrfStatusCode = responseCsrf.getStatusCode();
var csrf = responseCsrf.getHeader('x-csrf-token');
var cookies = responseCsrf.getHeader('Set-Cookie');
}
function reqcharm(csrf,cookies){
var reqCharm = new sn_ws.RESTMessageV2("SAP-SOLMAN Integration API', 'Create Charm Id"); 
reqCharm.setRequestHeader("x-csrf-token", csrf); 
var cookies = cookies.split(';');
     for (var i = 0; i < cookies.length; i++) {
        var cookie = cookies[i].trim();
        reqCharm.setRequestHeader('Cookie', cookie);
     }
}

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards
Durga

Tushar
Kilo Sage

Hi @KausikSharma 

something like this ?

 

(function executeRule(current, previous /*null when async*/) {
    // Create the first REST message to get CSRF token and cookies
    var reqCsrf = new sn_ws.RESTMessageV2('SAP-SOLMAN Integration API', 'Get CSRF');
    reqCsrf.setRequestHeader('x-csrf-token', 'fetch');

    // Execute the first REST request
    var responseCsrf = reqCsrf.execute();

    // Check if the request was successful
    if (responseCsrf && responseCsrf.getStatusCode() === 200) {
        var csrf = responseCsrf.getHeader('x-csrf-token');
        var cookiesHeader = responseCsrf.getHeader('Set-Cookie');

        if (csrf) {
            // Create the second REST message to create Charm Id
            var reqCharm = new sn_ws.RESTMessageV2('SAP-SOLMAN Integration API', 'Create Charm Id');
            reqCharm.setRequestHeader('x-csrf-token', csrf);

            // Apply each cookie from the Set-Cookie header to the second request
            if (cookiesHeader) {
                var cookies = cookiesHeader.split(';');
                for (var i = 0; i < cookies.length; i++) {
                    var cookie = cookies[i].trim();
                    reqCharm.setRequestHeader('Cookie', cookie);
                }
            }

            // Set the request body and execute the second REST request
            // ... (uncomment and fill in the rest of your request logic)

            // var resCharm = reqCharm.execute();
            // var charmBody = resCharm.getBody();
            // var charmStatusCode = resCharm.getStatusCode();
            // gs.error('--> Hello Kausik ' + charmStatusCode + charmBody);
        }
    } else {
        // Handle the case where the first REST request failed
        gs.error('Failed to fetch CSRF token and cookies.');
    }
})(current, previous);

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar