How to add the JSESSIONID and CSRFTOKEN inside the cookie through script include?

Bhagyashree Mo1
Kilo Contributor

Hello,

We have created the script include in order to call the "rest message followed by rest method" but we would further like to know that how we can pass the JSESSIONID and CSRFTOKEN inside the cookie through script. We anyway have the JSESSIONID stored in our instance browser but we need to pass it through script because we want this to hit the third party API (end point defined in the rest message). Thus, could you please share your suggestion/understanding on this?

Your help on this would be much appreciated!

Thanking in advance.

5 REPLIES 5

asifnoor
Kilo Patron

Hi,

The script includes work at the server level and from there you cannot set the values in the cookie which are at the client side.

However, you can capture the response of the script include and based on that in your client script you can set the cookies using plain javascript.

Mark the comment as a correct answer and also helpful if it answers your question.

Could you please help with some example? That would be much helpful!

Hi

Refer to this link which should help you to fix the issue.

https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/script/client_scripts/reference/r...

Kindly mark the comment as a correct answer and also helpful if it answers your question.

Hello,

JFyi, i would like to let you know that we are NOT looking for sessionID(gs.getSession()) but we are talking here about JSESSIONID.

However, just to further advise you on how to fetch or see the JSESSIONID, incase if you are not aware - so in our instance URL - before "https://" we do have LOCK icon , when we click on it ->navigate to cookies -> drop down on the respective opened ServiceNow instance name -> Cookies -> and you can see JSESSIONID below.

So , we do have JSESSIONID available in the cookies but we need to pass it through script because we want this to hit the third party API and thus we need to understand how we can further proceed on achieving it! 

Also just fyi, we are also fetching the CSRF token through script and passing the same in cookie - in order to set the request header. So the CSRF token in passed and we get to see the exact value in logs as well but unable to pass the JSESSIONID in the cookie on the request header. Please let me know if you have an idea on this case!

Thanking your time!