How do I store my token in variable?

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

Here’s my Token response:

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

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

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!

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)

1 Like

Hi @@Jean-Pierre bougie

Go through below link, this would be helpful for you

1 Like

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

1 Like

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

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, :slight_smile: :)**

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