# How do I store my token in variable?

**URL:** <https://forum.katalon.com/t/how-do-i-store-my-token-in-variable/14402>\
**Category:** API Testing\
**Tags:** api-testing, katalon-studio\
**Created:** [November 23, 2018, 5:39am UTC](https://forum.katalon.com/t/how-do-i-store-my-token-in-variable/14402 "2018-11-23T05:39:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jean2](https://avatars.discourse-cdn.com/v4/letter/j/a4c791/32.png) [@Jean2](https://forum.katalon.com/u/Jean2)\
**Post date:** [November 23, 2018, 5:39am UTC](https://forum.katalon.com/t/how-do-i-store-my-token-in-variable/14402/1 "2018-11-23T05:39:51Z")

</div>

Hello, I’m having difficulties in storing my token in variable. Could you please help me?

Here’s my Token response:

 ![](https://dub1.discourse-cdn.com/katalon/uploads/default/original/1X/e266d6e265aa8ca2eb7340973eafcd27228a3f94.png)

What I want to do is store my token in a variable. In that way, I can just call the variable token whenever I needed it in other API.

Thanks,  
Jean

[image.png](https://forum.katalon.com/uploads/short-url/wiQeVgdbYCRnlEFCvnrdrIiaiYQ.png)

---

<div class="post-metadata">

**Author:** ![Valeriy](https://avatars.discourse-cdn.com/v4/letter/v/5e9695/32.png) [@Valeriy](https://forum.katalon.com/u/Valeriy)\
**Post date:** [November 23, 2018, 3:25pm UTC](https://forum.katalon.com/t/how-do-i-store-my-token-in-variable/14402/2 "2018-11-23T15:25:07Z")

</div>

As I know, there are an option to use jsonslurper to parse a response  
And then you could create a global variable and save the content of “access\_token” element in it

---

<div class="post-metadata">

**Author:** ![Jean2](https://avatars.discourse-cdn.com/v4/letter/j/a4c791/32.png) [@Jean2](https://forum.katalon.com/u/Jean2)\
**Post date:** [November 26, 2018, 2:52am UTC](https://forum.katalon.com/t/how-do-i-store-my-token-in-variable/14402/3 "2018-11-26T02:52:00Z")

</div>

Hello Valeriy, I tried using jsonslurper but it didn’t work for me… Are there any other solutions? Can you please give some example? Thank You!

---

<div class="post-metadata">

**Author:** ![Valeriy](https://avatars.discourse-cdn.com/v4/letter/v/5e9695/32.png) [@Valeriy](https://forum.katalon.com/u/Valeriy)\
**Post date:** [November 26, 2018, 12:41pm UTC](https://forum.katalon.com/t/how-do-i-store-my-token-in-variable/14402/4 "2018-11-26T12:41:02Z")

</div>

import groovy.json.JsonSlurper  
…  
class AuthenticationAssertion {  
private static void Assert (UserName, Password){  
…  
def shitresponse = WS.sendRequest(findTestObject(‘API/Test/UserLogin’, [(‘Login’): UserName, (‘Password’): Password])).getResponseText()

```
  JsonSlurper slurper = new JsonSlurper()

  Map parsedJson = slurper.parseText(shitresponse)

  GlobalVariable.Auth_SessionID = parsedJson.sessionId  

```

–  
Here API/Test/UserLogin - path to a POST request:  
{“pool”:{“email”: “${Login}”, “password”: “${Password}”}}  
the same as have to be send when user trying to log in.  
First I define a variable for response. I had big troubles first time, forgive about variable names.  
GlobalVariable.Auth\_SessionID = parsedJson.\*\*sessionId \*\*  
Here sessionId is just a element in the JSON response (like yours access\_token)

---

<div class="post-metadata">

**Author:** ![Siva1](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/siva1/32/398_2.png) [@Siva1](https://forum.katalon.com/u/Siva1)\
**Post date:** [November 26, 2018, 12:43pm UTC](https://forum.katalon.com/t/how-do-i-store-my-token-in-variable/14402/5 "2018-11-26T12:43:25Z")

</div>

Hi @[@Jean-Pierre bougie](http://forum.katalon.com/profile/13016/Jean%20)

Go through below link, this would be helpful for you

> **[Salesforce API Testing with Katalon(dynamically handle OAuth token by script)](http://forum.katalon.com/discussion/10881/salesforce-api-testing-with-katalon-dynamically-handle-oauth-token-by-script#latest)**
>
> Hi All, I hope this article would be helpful for all salesforce automation testers.

---

<div class="post-metadata">

**Author:** ![Siva1](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/siva1/32/398_2.png) [@Siva1](https://forum.katalon.com/u/Siva1)\
**Post date:** [November 26, 2018, 12:43pm UTC](https://forum.katalon.com/t/how-do-i-store-my-token-in-variable/14402/6 "2018-11-26T12:43:49Z")

</div>

> Jean said:
> 
> Hello Valeriy, I tried using jsonslurper but it didn’t work for me… Are there any other solutions? Can you please give some example? Thank You!

Go through below link, this would be helpful for you

[http://forum.katalon.com/discussion/10881/salesforce-api-testing-with-katalon-dynamically-handle-oauth-token-by-script#latest](http://forum.katalon.com/discussion/10881/salesforce-api-testing-with-katalon-dynamically-handle-oauth-token-by-script#latest "Link: http://forum.katalon.com/discussion/10881/salesforce-api-testing-with-katalon-dynamically-handle-oauth-token-by-script#latest")

---

<div class="post-metadata">

**Author:** ![Jean2](https://avatars.discourse-cdn.com/v4/letter/j/a4c791/32.png) [@Jean2](https://forum.katalon.com/u/Jean2)\
**Post date:** [November 29, 2018, 1:04am UTC](https://forum.katalon.com/t/how-do-i-store-my-token-in-variable/14402/7 "2018-11-29T01:04:37Z")

</div>

Thank you so much for the help Guys! I really appreciated it.

---

<div class="post-metadata">

**Author:** ![Kritsanut](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kritsanut/32/512_2.png) [@Kritsanut](https://forum.katalon.com/u/Kritsanut)\
**Post date:** [November 29, 2018, 8:56am UTC](https://forum.katalon.com/t/how-do-i-store-my-token-in-variable/14402/8 "2018-11-29T08:56:24Z")

</div>

I have sample to you,

\*\*My Scenario \*\*HTTP PostBody Header  
**Step 1** Login for get Token  
**Step 2** Send Token to GetNews APIs

**TestCase script below**

**’Call Login URL’**

def Login = WS.sendRequest(findTestObject(‘Login/Login’, [(‘username’) : ‘YouUsername’, (‘password’) : ‘YouPassword’]))

**’Parse Json’**

def jsonSlurper = new JsonSlurper()

Login = jsonSlurper.parseText(Login.getResponseText())

Authtoken = Login.token

**’Print Token’**

println(Authtoken)

**’Print Token To Katalon Report’**

logger.logPassed(Authtoken)

**’POST HTTPHeader’**

def GetNews = ((findTestObject(‘News/Get\_News\_List’)) as RequestObject)

def HTTPHeader = new ArrayList()

HTTPHeader.add(new TestObjectProperty(‘Content-Type’, ConditionType.EQUALS, ‘application/json’))

HTTPHeader.add(new TestObjectProperty(‘Authorization’, ConditionType.EQUALS, “$Authtoken”))

GetNews.setHttpHeaderProperties(HTTPHeader)

**’Make POST Request’**

response = WS.sendRequest(GetNews)

**’Parse Json’**

def jsonSlurper1 = new JsonSlurper()

response = jsonSlurper.parseText(response.getResponseText())

GetnewsResponse = response.data

**’Print Json News Response’**

println(GetnewsResponse.toString())  
\*\*

Good luck SIr, 🙂 :)\*\*

---

<div class="post-metadata">

**Author:** ![gsp](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/gsp/32/20464_2.png) [@gsp](https://forum.katalon.com/u/gsp)\
**Post date:** [September 18, 2020, 8:32pm UTC](https://forum.katalon.com/t/how-do-i-store-my-token-in-variable/14402/9 "2020-09-18T20:32:16Z")

</div>

Hi @Kritsanut i am also facing this problem, how to capture acess\_token and save it in variable for another request as Bearer + Access\_token

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