Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events

Issue in getting cookies from AEM servlet

Avatar

Level 3

Hi all,

 

We have our website running on AEM 6.5 AMS which is interacting with Intershop via a shared cookie. Once the user is logged into Intershop, a cookie is set by Intershop for the loggedIn user details. This cookie is leveraged by AEM to show the loggedIn user and cart info on AEM pages. AEM has a servlet (invoked from topnav) which reads through all the cookies in request and matches cookie name and then updates the header. This is working fine in AEM 6.5 currently.

 

We are now migrating the website to AEMaaCS and on our cloud dev environment, we are having issue that even though the cookie is correctly set by Intershop but when the servlet is invoked from AEM, it is not getting the cookie using request.getCookies.

 

pardeepg4829047_0-1717562546447.png

 

I checked and the servlet call has cookie present in request headers but in below code, it is not retreived.

pardeepg4829047_1-1717562669315.png

 

 

Below is the code from servlet to read the cookies

public static JsonObject getCookieAsJson(HttpServletRequest request, String name) {
Cookie[] cookies = request.getCookies();
JsonObject json = new JsonObject();
if (cookies != null) {
for (Cookie cookie : cookies) {
LOG.info("cookie name is:" + cookie.getName());
if (cookie.getName().equals(name)) {
JsonParser parser = new JsonParser();
json = parser.parse(cookie.getValue()).getAsJsonObject();
}
}
}
return json;
}
Topics

Topics help categorize Community content and increase your ability to discover relevant content.

4 Replies

Avatar

Community Advisor

Hi @pardeepg4829047 
Are you able to see any of the cookie in the backend? i.e.

LOG.info("cookie name is:" + cookie.getName());


Arun Patidar

Avatar

Community Advisor

I am not sure about this but the only thing I noticed is size, can you try to reduce cookie size and check?



Arun Patidar

Avatar

Level 3

We changed the logic to read request.getHeader("Cookie") and then match the exact cookie name. This approach worked for us but not sure why the original code of request.getCookies() which was working fine on AEM 6.5 is unable to read the cookie in AEMaaCS.