Question
The Hongkong and Shanghai Banking Corporation Ltd India
IN
Last activity: 21 Nov 2024 1:19 EST
How to retrieve browser cookies in pega
Hi Team,
We have a requirement in which we have to read cookies to fetch the token and pass the token to another rest api.
***Edited by Moderator Marije to add Capability tags***
-
Reply
-
Share this page Facebook Twitter LinkedIn Email Copying... Copied!
Pegasystems Inc.
GB
@GarimaM0does the documentation help at all?
To read cookies and fetch the token in Pega you can utilize the cookie that is returned when the service runs as an authenticated user. When the session state in a service package is set to Stateful the service returns a cookie in the response with the Set Cookie HTTP header. You will need to include this cookie in the header of your subsequent requests to access the token. Ensure that you have the correct cookie handling in place to manage the session effectively.
⚠ This is a GenAI-powered tool. All generated answers require validation against the provided references.
The Hongkong and Shanghai Banking Corporation Ltd India
IN
@MarijeSchillern For us the requirement is to call an api via ajax and in response this ap will return a token inside the cookie which will be stored in the browser.
This ajax call will be made during user interaction, its not at the time of user SSO authentication or login.
Through ajax call the token is getting fetched and is stored in the browser cookies, challenge is to read this to read the token from the cookies.
when we are trying to read the cookies using javascript, its just returning 1 cookie -
Pega-Perf | itkn=6 |
whereas there are multiple cookies available in the same session in the browser.
Pegasystems Inc.
IN
@GarimaM0 The issue of only being able to read one cookie, specifically the Pega-Perf cookie, while multiple cookies are available in the session may be related to how cookies are configured in the response from the server. To read multiple cookies from a Connect REST response, you need to reconfigure the Set-Cookie header in the response. This allows for the possibility of reading multiple cookies. It is recommended to check the response configuration and ensure that the cookies are being set correctly in the response headers. Additionally, using the 'maintain session' option on the connector rule may help in managing multiple cookies effectively.
The Hongkong and Shanghai Banking Corporation Ltd India
IN
@thulg the cookies are not getting fetch using connect rest, its getting fetched using ajax
Updated: 19 Nov 2024 9:24 EST
Capgemini
NO
Hi @GarimaM0,
I believe there could be several reasons why you aren't able to retrieve all the cookies in your browser. I hope you're using document.cookie in JavaScript to fetch the cookies.
1. SameSite Cookie Attribute: Some cookies may have the SameSite attribute set, which can restrict their visibility in certain contexts (e.g., cross-site requests). Ensure that the cookies you expect to retrieve are not restricted by this attribute.
2. HttpOnly Cookies: Cookies marked as HttpOnly cannot be accessed via JavaScript for security reasons. If you're trying to access such cookies, they won't appear in document.cookie.
3. Browser Restrictions: Some browsers have strict privacy settings that may prevent certain cookies from being accessed. Check your browser settings to ensure cookies are enabled.
If you want to double confirm the list of cookies, you can use the below code in your console/code to verify as Json
function getAllCookies() {
const cookies = document.cookie.split('; ');
const cookieObject = {};
cookies.forEach(cookie => {
const [key, value] = cookie.split('=');
cookieObject[key] = decodeURIComponent(value);
});
return cookieObject;
}
const allCookies = getAllCookies();
console.log(allCookies);
Capgemini
IN
Hi @duraisankar ,
As you said HttpOnly type cookie cannot be accessed via JavaScript , do we have any alternative way to retrieve this HttpOnly cookie from Pega?
We have a design restriction to get this token generation API through AJAX call only and response will be received as in cookie. We have to find a way to capture it through Pega.
Capgemini
NO
@SOUVIKPEGA If you are using AJAX, you can try adding withCredentials
in your AJAX configuration. Here’s an example:
$.ajax({
url: 'https://example.com/api', // Replace with your cross-origin URL
method: 'GET',
xhrFields: {
withCredentials: true // This sends cookies with the request
},
success: function(data) {
console.log(data);
}
});
Make sure that the server allows cross-origin requests and is configured to accept credentials. This is essential for the cookies to be sent properly.
Capgemini
IN
Hi @duraisankar ,
Thank you for suggestion. However using this code we are still not able to capture cookie.
Could you please provide us any sample code, with which we can set the cookie in a Pega Clipboard property.
If it does not work with Javascript , can we alternatively capture the cookie using Java step in Activity ?
Thanks