cancel
Showing results for 
Search instead for 
Did you mean: 

Why can't I get cookies' values in XSJS?

lbersier
Explorer
0 Kudos

Hi experts,

I'm trying to get the login cookies within my XSJS, but the values I get from $.request.cookies are empty.


For instance, look at this sample code:

let output = "";
for (let name in $.request.cookies) {
output += "name:" + name + ",value" + JSON.stringify(cookies[name]) + "<br>"; } $.response.contentType = "text/html"; $.response.setBody(output);

After login, the browser shows:

name:0,value:{"name":"xsIdD2BEE...", value:""}
name:1,value:{"name":"sapxslb",value:""}

Which means I can get cookies' keys ("xsIdD2BEE" and "sapxslb"), but cookies' values are empty.

I can see the values in chrome's developer panel and they are not empty.

Can any one tell me how can I get cookies' values?

Thank you very much in advance!

Accepted Solutions (0)

Answers (2)

Answers (2)

ibibhu
Product and Topic Expert
Product and Topic Expert
0 Kudos

It appears that the issue may be caused by the way you are accessing the cookies. Instead of using $.request.cookies, try using $.getCookies(). This will return an array of cookies, which you can then loop through to access the name and value of each cookie. Alternatively, you can access a specific cookie by its name using $.getCookie(name), where name is the name of the cookie you want to retrieve. Example:

let cookies = $.getCookies();
for (let i = 0; i < cookies.length; i++) {
    let cookie = cookies[i];
    output += "name:" + cookie.name + ",value" + JSON.stringify(cookie.value) + "<br>";
}

or
let cookieValue = $.getCookie("xsIdD2BEE..."); console.log(cookieValue);
yogananda
Product and Topic Expert
Product and Topic Expert
0 Kudos

lbersier

Cookies in XSJS are not supported. XSJS is a server-side language that is used to develop and implement business logic on the SAP HANA platform, and cookies are a client-side feature that is used to store and retrieve data from the user's web browser.

lbersier
Explorer
0 Kudos

Hi Yogananda,
Thanks for your response.

If cookies are not supported in XSJS, why both $.web.WebRequest and $.web.WebResponse have a cookies member?

Are you saying that no matter what the documentation says, we might be able to see the request's cookie names via $.resquest.cookies[i].name but we'll NEVER get their values with $.resquest.cookies[i].value?

Side note: I did try setting up cookies in the response and that works fine; I get a nice and expected set-cookie