on 12-28-2022 4:54 PM
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!
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);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
User | Count |
---|---|
81 | |
9 | |
9 | |
7 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.