How to set a global variable from an API respond value (Authentication token)

Hello,
Does anyone know the best way to set a global variable with the respond token for my authentication. I want to call the global variable toke to be used on other calls. I need to store the bearer token for other calls. At this type I have my authentication call and storing the token in a variable. Thanks!

def response = WS.sendRequest(findTestObject(‘Object /Authentication’))

def jsonSlurper = new JsonSlurper()

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

Token = res.bearer_token

WS.verifyResponseStatusCode(response, 200)

println(Token)

Hi Rick,

this way should work:

import internal.GlobalVariable

//

GlobalVariable.yourTokenVar = Token 

Please remember that token is persisted only at runtime. If you want to persist token permanently (for different test case(suite) execution), you have to save it into permanent memory (file, database, …).

1 Like

Thanks for the info.
I’m getting the following error:

04-24-2018 01:14:48 PM - [END] - End action : Statement - println(AutToken)

04-24-2018 01:14:48 PM - [START] - Start action : Statement - AutToken = token

04-24-2018 01:14:48 PM - [END] - End action : Statement - AutToken = token

04-24-2018 01:14:48 PM - [ERROR] - Test Cases/Auth/TC02_Auth FAILED because (of) Variable ‘token’ is not defined for test case.

04-24-2018 01:14:48 PM - [END] - End Test Case : Test Cases/Auth/TC02_Auth

Here is my code:

Parses respond for token’

def jsonSlurper = new JsonSlurper()

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

AutToken = res.bearer_token

WS.verifyResponseStatusCode(response, 200)

println(AutToken)

GlobalVariable.AutToken = token

It looks like you’re confusing a local variable in your test with a GlobalVariable. Try this:

def jsonSlurper = new JsonSlurper()
def res = jsonSlurper.parseText(response.getResponseText())
AutToken = res.bearer_token
WS.verifyResponseStatusCode(response, 200)
println(AutToken)
GlobalVariable.AutToken = AutToken


1 Like

Thanks for the info.

I have the next problem :

est Cases/Advance examples/SaveToken FAILED because (of) org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:

file:/Users/fespinoza/Katalon%20Studio/Jira%20API%20Tests%20with%20Katalon%20Studio/Scripts/Advance%20examples/SaveToken/Script1533310109253.groovy: 25: unable to resolve class JsonSlurper

@ line 25, column 19.

def jsonSlurper = new JsonSlurper()

                 ^

1 error

how make for correct thist is?

Import is missing.

import groovy.json.JsonSlurper

How can I make my token wrong?

How do I tell which is the Api from which the token will be stored?

I am not sure what your question is. To authorize your API requests, you need:
- authentication endpoint (which usually returns authorization token in response)
- parse this response using JsonSlurper (get token itself)
- put token into Authorization HTTP header for your endpoints

So, I have to create a web service request object and inside that object write the token?

Screen Shot 2018-08-03 at 11.04.29 AM.png

this is working
but what is the way to use this autToken in other api as parameters??
And plz share GET request snapshot to use this variable

How do i do this automatically to apply to a global variable which can be applied to all the api tests?

As Marek_Melocik says above, to persist a variable, you have to save to permanent memory, so review the below:

Hey, thanks for responding.
I got 2 out of 3… but for #3, I need that token to be apply to a global variable automatically… there’s got to be a way in Katalon. Postman has a easy config for it. And my token that is returned is dynamic… it expires within 1 day and obviously a new one is generated after sending the login endpoint.

  1. authentication endpoint (which usually returns authorization token in response)
    -2) parse this response using JsonSlurper (get token itself)
    3)- put token into Authorization HTTP header for your endpoints

Guys,
I’m still trying to apply the Token respond value to the globalVariable automatically after each test run. i created a globalvariable and have value as empty string… would like to have new token applied to globalVariable everytime I run the endpoint to get new token… so all my api tests can get that token and execute.
would appreciate anyone that can help me set this up.