# Store AUTH TOKEN in Global Variable

**URL:** <https://forum.katalon.com/t/store-auth-token-in-global-variable/10743>\
**Category:** API Testing\
**Tags:** api-testing, katalon-studio\
**Created:** [May 7, 2018, 2:33pm UTC](https://forum.katalon.com/t/store-auth-token-in-global-variable/10743 "2018-05-07T14:33:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Julian\_Wladyczynski](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@Julian\_Wladyczynski](https://forum.katalon.com/u/Julian_Wladyczynski)\
**Post date:** [May 7, 2018, 2:33pm UTC](https://forum.katalon.com/t/store-auth-token-in-global-variable/10743/1 "2018-05-07T14:33:30Z")

</div>

Hello,

I have a problem with storing in GlobalVariable my authorization  
token, which I want to use in other test cases.

I’ve tried to do this in Postman and it’s works perfectly,  
but not in Katalon…

What I want to do:

**[tc-1] POST auth\_token  
and store access\_token and refresh\_token in GlobalVariable**

**[tc-2] GET user list  
using access\_token**

**How code looks [tc-1]:**

**import**  **static** com.kms.katalon.core.checkpoint.CheckpointFactory._findCheckpoint_

**import**  **static** com.kms.katalon.core.testcase.TestCaseFactory._findTestCase_

**import**  **static** com.kms.katalon.core.testdata.TestDataFactory._findTestData_

**import**  **static** com.kms.katalon.core.testobject.ObjectRepository._findTestObject_

**import**  
com.google.common.util.concurrent.CycleDetectingLockFactory.ExampleStackTrace

**import**  
com.kms.katalon.core.checkpoint.Checkpoint **as** Checkpoint

**import**  
com.kms.katalon.core.checkpoint.CheckpointFactory **as** CheckpointFactory

**import**  
com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords **as** MobileBuiltInKeywords

**import**  
com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords **as** Mobile

**import**  
com.kms.katalon.core.model.FailureHandling **as**  
FailureHandling

**import**  
com.kms.katalon.core.testcase.TestCase **as**  
TestCase

**import**  
com.kms.katalon.core.testcase.TestCaseFactory **as**  
TestCaseFactory

**import**  
com.kms.katalon.core.testdata.TestData **as**  
TestData

**import**  
com.kms.katalon.core.testdata.TestDataFactory **as**  
TestDataFactory

**import**  
com.kms.katalon.core.testobject.ObjectRepository **as** ObjectRepository

**import**  
com.kms.katalon.core.testobject.TestObject **as**  
TestObject

**import**  
com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords **as** WSBuiltInKeywords

**import**  
com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords **as** WS

**import**  
com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords **as** WebUiBuiltInKeywords

**import** com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords  
**as** WebUI

**import** internal.GlobalVariable **as** GlobalVariable

**import** groovy.json.JsonSlurper **as** JsonSlurper

**import**  
com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords **as** WS

**import** com.kms.katalon.core.testobject.TestObjectProperty

**import**  
com.kms.katalon.core.testobject.ConditionType

‘Send a REST request and return its response’

**def** response = WS._sendRequest_(_findTestObject_(‘Object  
Repository/QA\_POST\_AUTH\_TOKEN’))

‘Store access\_token’

**def** jsonSlurper = **new** JsonSlurper()

**def** res = jsonSlurper.parseText(response.getResponseText())

AutToken = res.access\_token

println AutToken

GlobalVariable._AutToken_ = AutToken

As I can see in console, access token is printable. But when  
I try to run **[tc-2]** there is no  
AutToken (the string is empty)

In case of **Postman** , I’ve used this:

var jsonData =  
JSON.parse(responseBody);

postman.setGlobalVariable(“access\_token”,  
jsonData.access\_token);

And access\_token is stored in GlobalVariable, which I can  
use in next test case…

I’ve tried everything what I could find in Katalon forum and  
in the internet

---

<div class="post-metadata">

**Author:** ![Julian\_Wladyczynski](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@Julian\_Wladyczynski](https://forum.katalon.com/u/Julian_Wladyczynski)\
**Post date:** [May 7, 2018, 2:51pm UTC](https://forum.katalon.com/t/store-auth-token-in-global-variable/10743/2 "2018-05-07T14:51:16Z")

</div>

(and I forgot to add)

It seems to me, that GlobalVariable works like local  
variables…

I’m using Katalon Studio Version: 5.4.1 on Windows 10

---

<div class="post-metadata">

**Author:** ![Trong\_Bui](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/trong_bui/32/374_2.png) [@Trong\_Bui](https://forum.katalon.com/u/Trong_Bui)\
**Post date:** [May 9, 2018, 8:02am UTC](https://forum.katalon.com/t/store-auth-token-in-global-variable/10743/3 "2018-05-09T08:02:10Z")

</div>

Julian,

Global variables can be set new value at run-time, and technically, this new value is only available at run-time. If you get the token, update to Global variable and use this variable in the new request, then the request will be able to use the token.

---

<div class="post-metadata">

**Author:** ![ealiaj](https://avatars.discourse-cdn.com/v4/letter/e/13edae/32.png) [@ealiaj](https://forum.katalon.com/u/ealiaj)\
**Post date:** [July 22, 2019, 10:27pm UTC](https://forum.katalon.com/t/store-auth-token-in-global-variable/10743/4 "2019-07-22T22:27:09Z")

</div>

Is this working I’m trying to do the same exact thing but I can’t, I guess because it only persists for runtime, then disappears. What is the best practice for achieving this? Saving it to a file and reading the data?

---

<div class="post-metadata">

**Author:** ![s.blaszczykiewicz](https://avatars.discourse-cdn.com/v4/letter/s/7ba0ec/32.png) [@s.blaszczykiewicz](https://forum.katalon.com/u/s.blaszczykiewicz)\
**Post date:** [July 23, 2019, 11:18am UTC](https://forum.katalon.com/t/store-auth-token-in-global-variable/10743/5 "2019-07-23T11:18:46Z")

</div>

My investigation shows me that It seems to GlobalVariables are stored and transfer (between test cases) only when You run a TestSuite and there is possible to use a GlobalVariable in REST API HTTP Header (GUI).  
To use just call ${variablename) and define them in variables in testcases and run through TestSuite.

 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/8/83e895c7ceaeb1a05c037d60f954b8e240e8ffc0.png)

PS. I also found something, but I can;t use this:  
String authHeader = “whateverYouNeedForAuthentication”  
TestObjectProperty header1 = new TestObjectProperty(“Authorization”, ConditionType.EQUALS, authHeader)  
TestObjectProperty header2 = new TestObjectProperty(“Content-Type”, ConditionType.EQUALS, “application/json”)  
TestObjectProperty header3 = new TestObjectProperty(“Accept”, ConditionType.EQUALS, “application/json”)  
ArrayList defaultHeaders = Arrays.asList(header1, header2, header3)

---

<div class="post-metadata">

**Author:** ![sang](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/sang/32/6357_2.png) [@sang](https://forum.katalon.com/u/sang)\
**Post date:** [July 23, 2019, 2:09pm UTC](https://forum.katalon.com/t/store-auth-token-in-global-variable/10743/6 "2019-07-23T14:09:34Z")

</div>

Hi [**Julian\_Wladyczynski**](http://forum.katalon.com/u/Julian_Wladyczynski)

Please retry by following this step:

**1. Assigned access\_token value to the GlobalVariable**

```
response = WS.sendRequest(findTestObject('Employees/Login backoffice and get access token'))
WS.verifyResponseStatusCode(response, 200)
JsonSlurper slurper = new JsonSlurper()
Map parsedJson = slurper.parseText(response.getResponseText())
String access_token = parsedJson.access_token
GlobalVariable.Token = access_token

```

**2. On the GET request, set the Variable as below.**

 ![38%20PM](https://europe1.discourse-cdn.com/katalon/original/2X/6/615455098c602c7704a4929ce2a8e1a17c168dcb.png)

**3. Using the access\_token in the request header.**

 ![32%20PM](https://europe1.discourse-cdn.com/katalon/original/2X/4/4ae0c1196648fa451e2cd549b5cfd3fdd303a92f.png)

Hope this helps.

---

<div class="post-metadata">

**Author:** ![ealiaj](https://avatars.discourse-cdn.com/v4/letter/e/13edae/32.png) [@ealiaj](https://forum.katalon.com/u/ealiaj)\
**Post date:** [July 23, 2019, 4:10pm UTC](https://forum.katalon.com/t/store-auth-token-in-global-variable/10743/7 "2019-07-23T16:10:05Z")

</div>

Sang, do you know if I could use the same in the body? When I copied pasted the auth token in the body it worked, when I used the variable in the body it failed.

---

<div class="post-metadata">

**Author:** ![nitijabin](https://avatars.discourse-cdn.com/v4/letter/n/e99b99/32.png) [@nitijabin](https://forum.katalon.com/u/nitijabin)\
**Post date:** [July 23, 2019, 8:02pm UTC](https://forum.katalon.com/t/store-auth-token-in-global-variable/10743/8 "2019-07-23T20:02:45Z")

</div>

I am not getting your step 1. where to set the response

---

<div class="post-metadata">

**Author:** ![sang](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/sang/32/6357_2.png) [@sang](https://forum.katalon.com/u/sang)\
**Post date:** [July 24, 2019, 3:52am UTC](https://forum.katalon.com/t/store-auth-token-in-global-variable/10743/9 "2019-07-24T03:52:24Z")

</div>

From the test case script [nitijabin](http://forum.katalon.com/u/nitijabin)

---

<div class="post-metadata">

**Author:** ![ealiaj](https://avatars.discourse-cdn.com/v4/letter/e/13edae/32.png) [@ealiaj](https://forum.katalon.com/u/ealiaj)\
**Post date:** [July 31, 2019, 9:01am UTC](https://forum.katalon.com/t/store-auth-token-in-global-variable/10743/10 "2019-07-31T09:01:38Z")

</div>

For this I decided to just write to excel sheet I save the first token, call it in the next auth call, then store the authorization bearer token. Something else to consider.

---

<div class="post-metadata">

**Author:** ![Harini2rajanna](https://avatars.discourse-cdn.com/v4/letter/h/df705f/32.png) [@Harini2rajanna](https://forum.katalon.com/u/Harini2rajanna)\
**Post date:** [September 20, 2019, 5:05am UTC](https://forum.katalon.com/t/store-auth-token-in-global-variable/10743/11 "2019-09-20T05:05:57Z")

</div>

Hi Sang,

I m trying in the same way .

but not able to provide the token in authorization  
 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/8/8c1630a83f5033343fc2ab7f8ca60fcb4ce6f593.png)

facing

{  
“message”:“Unauthorized”,  
“code”:“401”,  
“reason”:“Invalid OAuth token provided.”  
}

---

<div class="post-metadata">

**Author:** ![sang](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/sang/32/6357_2.png) [@sang](https://forum.katalon.com/u/sang)\
**Post date:** [September 20, 2019, 11:56am UTC](https://forum.katalon.com/t/store-auth-token-in-global-variable/10743/12 "2019-09-20T11:56:57Z")

</div>

Hi [Harini2rajanna](http://forum.katalon.com/u/Harini2rajanna)

Please recheck all steps to make sure that it is correct, or print the access token variable value to see if you got it.

---

<div class="post-metadata">

**Author:** ![Harini2rajanna](https://avatars.discourse-cdn.com/v4/letter/h/df705f/32.png) [@Harini2rajanna](https://forum.katalon.com/u/Harini2rajanna)\
**Post date:** [September 22, 2019, 7:56am UTC](https://forum.katalon.com/t/store-auth-token-in-global-variable/10743/13 "2019-09-22T07:56:31Z")

</div>

Hi Sang,

Please see below with screen-shots

1. I am assigning the token to global variable  
 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/1/18b97215204be7299797d84afb1185592333fe59.png)

2.Declared the Acess\_token in default as a global varaible

 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/c/c187c1a1ac77f1a95c94089dfc662f1382e49bd9.png)

1. In another post call I am calling the global variable in header

 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/9/9e7fd5e62964db0e49b71fce3e9a761b33418c5c.png)

when I print it its a empty string

Note: I am doing all this operations on object repository

Not sure what I am doing wrong here  
Please assist me on this

---

<div class="post-metadata">

**Author:** ![Ibus](https://avatars.discourse-cdn.com/v4/letter/i/e9a140/32.png) [@Ibus](https://forum.katalon.com/u/Ibus)\
**Post date:** [September 22, 2019, 9:46am UTC](https://forum.katalon.com/t/store-auth-token-in-global-variable/10743/14 "2019-09-22T09:46:17Z")

</div>

this will never work.  
you have to do the setvariable \> grab variable from the testcase, not from the request object.  
and put the testcases in a testsuite.

let me guess, you are a Postman user? 🙂  
katalon is not working like this. the instance of the profile (global) class used when you run the tests manually it is different than the instance used by the GUI.  
when you run manually the tescases (or you run the request object) the GUI instance of the global class is not updated, it can be read only. (in fact, i think the associated xml file is read … not sure how is working under the hood)

when you run a request, or a testcase, a new instance of the class is created, having the default values as defined in the GUI one. but this  
object is scoped to the runtime.  
so, when, you run manually a request (or a testcase) \> a new instance is created. the request is ok, or the testcase run with success \> all fine and the instance is destroyed. or they fail … whatever. the runtime scope is ended so the created ‘in memory’ objects are destroyed.  
you run the second request or testcase \> the above process is repeated. so, the global exported in step one is not preserved for step two.

when you run a testuite, the instance of the global class created is scoped to the suite, so the value set by first testcase can be grabbed in the second testcase.

hope that helped to understand why katalon behaviour \<\> postman behaviour

---

<div class="post-metadata">

**Author:** ![Harini2rajanna](https://avatars.discourse-cdn.com/v4/letter/h/df705f/32.png) [@Harini2rajanna](https://forum.katalon.com/u/Harini2rajanna)\
**Post date:** [September 22, 2019, 11:43am UTC](https://forum.katalon.com/t/store-auth-token-in-global-variable/10743/15 "2019-09-22T11:43:04Z")

</div>

Thank you.  
Yes I am a postman user.  
This helped me lot .

---

<div class="post-metadata">

**Author:** ![Harini2rajanna](https://avatars.discourse-cdn.com/v4/letter/h/df705f/32.png) [@Harini2rajanna](https://forum.katalon.com/u/Harini2rajanna)\
**Post date:** [September 22, 2019, 12:57pm UTC](https://forum.katalon.com/t/store-auth-token-in-global-variable/10743/16 "2019-09-22T12:57:56Z")

</div>

As u said

1.I grab the value from test case and assign it to Global variable

 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/1/1af128a1f9a4d24636435ef3343a43ce48b1bbe2.png)  
2. I have to pass this value in header of next request  
in the object repository  
 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/9/984c6bd488ab117986202391215b26abcb7e27fd.png)  
3. Create the empty global value  
 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/7/7e8bbbbcbe46ad8315f6d65e7c9d8553176c82fd.png)  
4. Both the test case are running in test suite  
 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/7/74bd95c7f09c860cadca95190136aa17a7dc2dfa.png)

facing the same 401 token is invalid

---

<div class="post-metadata">

**Author:** ![Ibus](https://avatars.discourse-cdn.com/v4/letter/i/e9a140/32.png) [@Ibus](https://forum.katalon.com/u/Ibus)\
**Post date:** [September 22, 2019, 2:07pm UTC](https://forum.katalon.com/t/store-auth-token-in-global-variable/10743/17 "2019-09-22T14:07:28Z")

</div>

dont pass the value straight to the request object … but from the second testcase, call the request object with parametters binding.  
helooo … should i explain also the variables visibility? is in the docs.  
request objects are only for design. i repeat. forget about postman

---

<div class="post-metadata">

**Author:** ![Harini2rajanna](https://avatars.discourse-cdn.com/v4/letter/h/df705f/32.png) [@Harini2rajanna](https://forum.katalon.com/u/Harini2rajanna)\
**Post date:** [September 23, 2019, 1:09am UTC](https://forum.katalon.com/t/store-auth-token-in-global-variable/10743/18 "2019-09-23T01:09:22Z")

</div>

Hey can you share me the docs or in detail explain me how do i need to pass it in 2 test case  
It will be really helpful

Thank you

---

<div class="post-metadata">

**Author:** ![Harini2rajanna](https://avatars.discourse-cdn.com/v4/letter/h/df705f/32.png) [@Harini2rajanna](https://forum.katalon.com/u/Harini2rajanna)\
**Post date:** [September 23, 2019, 2:09am UTC](https://forum.katalon.com/t/store-auth-token-in-global-variable/10743/19 "2019-09-23T02:09:43Z")

</div>

Its working for me thank you

---

<div class="post-metadata">

**Author:** ![Harini2rajanna](https://avatars.discourse-cdn.com/v4/letter/h/df705f/32.png) [@Harini2rajanna](https://forum.katalon.com/u/Harini2rajanna)\
**Post date:** [September 24, 2019, 12:31am UTC](https://forum.katalon.com/t/store-auth-token-in-global-variable/10743/20 "2019-09-24T00:31:51Z")

</div>

Its working for me

Thank you

[Next page](https://forum.katalon.com/t/store-auth-token-in-global-variable/10743.md?page=2)
