Pass cookie as a variable to the API request

Hi,

I have lot of API requests where the cookies change every day. Instead of going and updating each API request , I want to create a global variable for the Cookie’s value and update that variable every time,so that my requests can read that variable and run.

Is it possible to do this?

prashant said:

Hi,

I have lot of API requests where the cookies change every day. Instead of going and updating each API request , I want to create a global variable for the Cookie’s value and update that variable every time,so that my requests can read that variable and run.

Is it possible to do this?

Prashant,

Could you try our new release 5.4 with following feature https://docs.katalon.com/display/KD/Parameterize+a+Web+Service+object?
Hope that it helps to solve your case.

Hi,

Thanks for the reply.
It didnt exactly solve my problem , I can now add a variable in the Web service object and then run from my test cases. But this means now in each test case I have to add the actual cookie.

It would have been better if I could use a global variable so that I can replace the cookie value once a day(morning) and then all cases gets automatically the latest value when the cases/s are run.

Also I feel in this way, I cant test the Web service Object properly till I create a test case because I can add the variable in it but run./test it only when a case calls it!!

Is it possible to just use a global variable in the web service object and then call from test case the web service request which will run using the global variable.

prashant said:

Hi,

Thanks for the reply.
It didnt exactly solve my problem , I can now add a variable in the Web service object and then run from my test cases. But this means now in each test case I have to add the actual cookie.

It would have been better if I could use a global variable so that I can replace the cookie value once a day(morning) and then all cases gets automatically the latest value when the cases/s are run.

Also I feel in this way, I cant test the Web service Object properly till I create a test case because I can add the variable in it but run./test it only when a case calls it!!

Is it possible to just use a global variable in the web service object and then call from test case the web service request which will run using the global variable.

You can use Global Variable as well. Just assign that Global Variable to a variable you want to use in your test case, e.g:

GlobalVariable.gl_Cookies = myCookies
WS.sendRequest(findTestObject(''), [cookies: myCookies])

Hi,
Thanks for the reply…This works for me and i am able to have Global variable to be used as a replacement :slight_smile:

Thanks again

Sorry that I open this old topic, but I have a similar problem and doesn’t understand the solution.

I have to use the same cookie for many requests in one test case (in future for more test cases too) and I also thought to use global variables.

I’m using Katalon Studio 5.9.1 and defined a globale Variable at the Profile “default”.

<?xml version="1.0" encoding="UTF-8"?>
<GlobalVariableEntities>
   <description></description>
   <name>default</name>
   <tag></tag>
   <defaultProfile>true</defaultProfile>
   <GlobalVariableEntity>
      <description></description>
      <initValue>'JSESSIONID=HjvIZRgU6Tcd2fZTHDcG25VQ5mATJK4sqXtIdtJl; path=/my-service'</initValue>
      <name>myCookies</name>
   </GlobalVariableEntity>
</GlobalVariableEntities>

Your solution was to set the globale Variable at the Testcase like

GlobalVariable.myCookies = ‘JSESSIONID=HjvIZRgU6Tcd2fZTHDcG25VQ5mATJK4sqXtIdtJl; path=/my-service’
WS.sendRequest(findTestObject(’’), [cookies: myCookies])

But when I execute my test case I got the following error:

Test Cases/InsertVertexAtVirtualNodes/Get Insert - Besser FAILED.
Reason:
groovy.lang.MissingPropertyException: No such property: myCookies for class: Script1544695618827

Do I have to define a variable at the Request too?

My request only consists of this url:

http://localhost:8080/my-service/myservlet?command=myMethode&myparam=xyz

Thank you for any hint.

Andreas

Hi Andreas,

tl;dr :

GlobalVariable.myCookies = ‘JSESSIONID=HjvIZRgU6Tcd2fZTHDcG25VQ5mATJK4sqXtIdtJl; path=/my-service’
WS.sendRequest(findTestObject(’’), [cookies: GlobalVariable.myCookies])

Explanation:

You are trying to access non-existing property called myCookies. The exception you get is telling the same.
The script tries to find local variable with name myCookies, which, obviously, doesn’t exist.

You changed variable GlobalVariable.myCookies - not local, but a part of GlobalVariable class. Ultimately you can create a local variable and assign your cookie there, but the code would be slightly different. See:

String myCookies = ‘JSESSIONID=HjvIZRgU6Tcd2fZTHDcG25VQ5mATJK4sqXtIdtJl; path=/my-service’
WS.sendRequest(findTestObject(’’), [cookies: myCookies])

One more thing worth to say - local variable does exist only within a single test case. You’d have to create it again and again. On the other hand, when you assign your cookie to a global variable at the start of your test suite, it is still set for all tests within the suite.

Thank you for your hint.

I tried to use both methodes (globale variable and local variable), but got following error.

Test Cases/InsertVertexAtVirtualNodes/Get Insert - Test FAILED.
Reason:
groovy.lang.MissingMethodException: No signature of method: static com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords.sendRequest() is applicable for argument types: (com.kms.katalon.core.testobject.RequestObject, java.util.LinkedHashMap) values: [TestObject - ‘Object Repository/TestRequest2/InsertVertex/InsertVertex-2’, …]
Possible solutions: sendRequest(com.kms.katalon.core.testobject.RequestObject), sendRequest(com.kms.katalon.core.testobject.RequestObject, com.kms.katalon.core.model.FailureHandling)

My code looks like (local variable):

String myCookies = ‘JSESSIONID=HjvIZRgU6Tcd2fZTHDcG25VQ5mATJK4sqXtIdtJl; path=/my-service’

WS.sendRequest(findTestObject(‘TestRequest2/InsertVertex/InsertVertex-2’), [(‘cookies’) : myCookies])

My code looks like (global variable):

GlobalVariable.myCookies = ‘JSESSIONID=HjvIZRgU6Tcd2fZTHDcG25VQ5mATJK4sqXtIdtJl; path=/my-service’

WS.sendRequest(findTestObject(‘TestRequest2/InsertVertex/InsertVertex-2’), [(‘cookies’) : GlobalVariable.myCookies])

Is the reason that the methode doesn’t support the use of the cookie anymore?

Now I thought I had a typo error, but after changing my code to

WS.sendRequest(findTestObject(‘TestRequest2/InsertVertex/InsertVertex-2’), [cookies: GlobalVariable.myCookies])

or

WS.sendRequest(findTestObject(‘TestRequest2/InsertVertex/InsertVertex-2’), [cookies: myCookies])

But I get the same error.

[Update]

Last night I could sleep, because I wanted this issue to be fixed.

After doing a little bit of search I found this

https://dzone.com/articles/create-rest-api-request-manually-using-katalon-stu

I only changed the header 1 part to my cookie like

String cookieHeader = GlobalVariable.myCookies

TestObjectProperty header1 = new TestObjectProperty(“Cookie”, ConditionType.EQUALS,cookieHeader)

And it worked.

Therefore I will go forward doing more testcases in the script mode. It is really powerful and I’m thinking that this mode is the big strenght of Katalon Studio.

Thanks again for your support. This forum is very useful and essential for me.

Andreas

2 Likes

Awesome! I am happy that you were able to find a solution.

Actually, this is exactly my opinion. I wasn’t able to help you with passing params to a request cause I haven’t used it since forever. :smiley: IMO script mode is the best mode for QA engineer, who is able to write (at least) a simple code. Straightforward and powerful, combining advantages of Katalon and Selenium.

Good job!

1 Like