How to fetch Cookies from one REST API and applied on other REST API?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2023 04:20 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2023 09:51 AM - edited 09-04-2023 09:51 AM
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());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2023 10:57 AM - edited 09-05-2023 01:07 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2023 03:06 PM
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