# How to create a test that saves the token of my API

**URL:** <https://forum.katalon.com/t/how-to-create-a-test-that-saves-the-token-of-my-api/12391>\
**Category:** API Testing\
**Tags:** api-testing, katalon-studio\
**Created:** [August 3, 2018, 3:45pm UTC](https://forum.katalon.com/t/how-to-create-a-test-that-saves-the-token-of-my-api/12391 "2018-08-03T15:45:33Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fernando\_Espinoza](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/fernando_espinoza/32/539_2.png) [@Fernando\_Espinoza](https://forum.katalon.com/u/Fernando_Espinoza)\
**Post date:** [August 3, 2018, 3:45pm UTC](https://forum.katalon.com/t/how-to-create-a-test-that-saves-the-token-of-my-api/12391/1 "2018-08-03T15:45:33Z")

</div>

I need to access my Api and then make GET AND POST requests and be able to show this result in the katalon. I attack any kind of help you can give me.

---

<div class="post-metadata">

**Author:** ![Andrej\_Podhajsky](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/andrej_podhajsky/32/1055_2.png) [@Andrej\_Podhajsky](https://forum.katalon.com/u/Andrej_Podhajsky)\
**Post date:** [August 6, 2018, 9:19am UTC](https://forum.katalon.com/t/how-to-create-a-test-that-saves-the-token-of-my-api/12391/2 "2018-08-06T09:19:08Z")

</div>

Hello,  
you just need to process headers from 1st response, and store token in variable so it can be used in construction of header in following request something like this:

```auto
def token = response.getHeaderFields().tokenCookie 
myCookie = new Cookie("token", token)
new Response = WS.sendRequest(findTestObject(ro.getObjectId(),[('COOKIE'): myCookie]))

```

i dont know how exacly your token looks like and if you can send it as cookie or you need to set it directly as headers this is request definition  
 ![](https://s3.amazonaws.com/katalon-forum/editor/4u/5jmpon1hvoby.png "Image: https://s3.amazonaws.com/katalon-forum/editor/4u/5jmpon1hvoby.png")

[image.png](https://s3.amazonaws.com/katalon-forum/editor/4u/5jmpon1hvoby.png)

---

<div class="post-metadata">

**Author:** ![Fernando\_Espinoza](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/fernando_espinoza/32/539_2.png) [@Fernando\_Espinoza](https://forum.katalon.com/u/Fernando_Espinoza)\
**Post date:** [August 6, 2018, 2:47pm UTC](https://forum.katalon.com/t/how-to-create-a-test-that-saves-the-token-of-my-api/12391/3 "2018-08-06T14:47:56Z")

</div>

thanks,  
where I have to write the code, I have to create a test case and define  
the path of the object get or pos where I wait for the token?

---

<div class="post-metadata">

**Author:** ![Andrej\_Podhajsky](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/andrej_podhajsky/32/1055_2.png) [@Andrej\_Podhajsky](https://forum.katalon.com/u/Andrej_Podhajsky)\
**Post date:** [August 6, 2018, 2:50pm UTC](https://forum.katalon.com/t/how-to-create-a-test-that-saves-the-token-of-my-api/12391/4 "2018-08-06T14:50:58Z")

</div>

> Fernando Espinoza said:
> 
> \*\*\*cut\*\*\* I have to create a test case and define  
> the path of the object get or pos where I wait for the token?

yes, exactly

---

<div class="post-metadata">

**Author:** ![Fernando\_Espinoza](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/fernando_espinoza/32/539_2.png) [@Fernando\_Espinoza](https://forum.katalon.com/u/Fernando_Espinoza)\
**Post date:** [August 6, 2018, 3:11pm UTC](https://forum.katalon.com/t/how-to-create-a-test-that-saves-the-token-of-my-api/12391/5 "2018-08-06T15:11:13Z")

</div>

How do I define the route to the object, is it right there?

**def** request = WS._sendRequest_(_findTestObject_(‘User/GetUserDetails’))

---

<div class="post-metadata">

**Author:** ![Andrej\_Podhajsky](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/andrej_podhajsky/32/1055_2.png) [@Andrej\_Podhajsky](https://forum.katalon.com/u/Andrej_Podhajsky)\
**Post date:** [August 6, 2018, 3:13pm UTC](https://forum.katalon.com/t/how-to-create-a-test-that-saves-the-token-of-my-api/12391/6 "2018-08-06T15:13:49Z")

</div>

that looks good… just small fix (wording) result of WS.sendRequest() is response - so you will not be mistaken by yourself when processing variable

---

<div class="post-metadata">

**Author:** ![Fernando\_Espinoza](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/fernando_espinoza/32/539_2.png) [@Fernando\_Espinoza](https://forum.katalon.com/u/Fernando_Espinoza)\
**Post date:** [August 6, 2018, 3:42pm UTC](https://forum.katalon.com/t/how-to-create-a-test-that-saves-the-token-of-my-api/12391/7 "2018-08-06T15:42:43Z")

</div>

I have one more question, how do you know this code, where will you get the token? that  
is, where do I define the API url, would I need to write a code where I  
wrote the credentials of the API in order to have the token and then  
tomasen the token a global variable?

---

<div class="post-metadata">

**Author:** ![Andrej\_Podhajsky](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/andrej_podhajsky/32/1055_2.png) [@Andrej\_Podhajsky](https://forum.katalon.com/u/Andrej_Podhajsky)\
**Post date:** [August 6, 2018, 3:51pm UTC](https://forum.katalon.com/t/how-to-create-a-test-that-saves-the-token-of-my-api/12391/8 "2018-08-06T15:51:41Z")

</div>

that depends on type of authentication. in my case i needed to authenticate against kerberos server, i used libraries for that purpose. if you have OAuth 1.0 you can set that directly in request definition, otherwise you need to use existing library or write your own.

---

<div class="post-metadata">

**Author:** ![wale\_Goold](https://avatars.discourse-cdn.com/v4/letter/w/b38774/32.png) [@wale\_Goold](https://forum.katalon.com/u/wale_Goold)\
**Post date:** [May 28, 2019, 8:02pm UTC](https://forum.katalon.com/t/how-to-create-a-test-that-saves-the-token-of-my-api/12391/9 "2019-05-28T20:02:09Z")

</div>

hi,  
I tried this code

```auto
def token = response.getHeaderFields().tokenCookie 
myCookie = new Cookie("token", token)
def newResponse = WS.sendRequest(findTestObject(ro.getObjectId(),[('COOKIE'): myCookie]))

```

but i am getting  
05-28-2019 10:00:31 PM - [START] - Start Test Case : Verification  
05-28-2019 10:00:32 PM - [ERROR] - Verification FAILED because (of) (Stack trace: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:  
WSVerification1559073632137: 22: unable to resolve class Cookie  
@ line 22, column 63.  
elds().tokenCookie myCookie = new Cookie  
^

1 error

```
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:946)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:593)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:542)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
at com.kms.katalon.core.main.ScriptEngine.getScript(ScriptEngine.java:188)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:183)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:108)
at com.kms.katalon.core.main.WSVerificationExecutor.runScript(WSVerificationExecutor.java:145)
at com.kms.katalon.core.main.WSVerificationExecutor.doExecute(WSVerificationExecutor.java:139)
at com.kms.katalon.core.main.WSVerificationExecutor.processExecutionPhase(WSVerificationExecutor.java:122)
at com.kms.katalon.core.main.WSVerificationExecutor.accessMainPhase(WSVerificationExecutor.java:114)
at com.kms.katalon.core.main.WSVerificationExecutor.execute(WSVerificationExecutor.java:102)
at com.kms.katalon.core.main.TestCaseMain.runWSVerificationScript(TestCaseMain.java:112)
at com.kms.katalon.core.main.TestCaseMain$runWSVerificationScript$0.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:149)
at TempTestCase1559073625443.run(TempTestCase1559073625443.groovy:22)
at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:263)
at groovy.lang.GroovyShell.run(GroovyShell.java:518)
at groovy.lang.GroovyShell.run(GroovyShell.java:507)
at groovy.ui.GroovyMain.processOnce(GroovyMain.java:653)
at groovy.ui.GroovyMain.run(GroovyMain.java:384)
at groovy.ui.GroovyMain.process(GroovyMain.java:370)
at groovy.ui.GroovyMain.processArgs(GroovyMain.java:129)
at groovy.ui.GroovyMain.main(GroovyMain.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:109)
at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:131)

```

)  
05-28-2019 10:00:32 PM - [END] - End Test Case : Verification

can you help

cheers

wale

---

<div class="post-metadata">

**Author:** ![Andrej\_Podhajsky](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/andrej_podhajsky/32/1055_2.png) [@Andrej\_Podhajsky](https://forum.katalon.com/u/Andrej_Podhajsky)\
**Post date:** [May 29, 2019, 5:22am UTC](https://forum.katalon.com/t/how-to-create-a-test-that-saves-the-token-of-my-api/12391/10 "2019-05-29T05:22:24Z")

</div>

you need to import libraries for cookies CTRL+SHIFT+O  
or

```auto
import org.openqa.selenium.Cookie

```

---

<div class="post-metadata">

**Author:** ![wale\_Goold](https://avatars.discourse-cdn.com/v4/letter/w/b38774/32.png) [@wale\_Goold](https://forum.katalon.com/u/wale_Goold)\
**Post date:** [May 29, 2019, 8:55am UTC](https://forum.katalon.com/t/how-to-create-a-test-that-saves-the-token-of-my-api/12391/11 "2019-05-29T08:55:48Z")

</div>

Thanks. now i get no such property error  
[ERROR] - Verification FAILED because (of) (Stack trace: groovy.lang.MissingPropertyException: No such property: token for class: WSVerification1559119880398  
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)  
at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:52)

---

<div class="post-metadata">

**Author:** ![Andrej\_Podhajsky](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/andrej_podhajsky/32/1055_2.png) [@Andrej\_Podhajsky](https://forum.katalon.com/u/Andrej_Podhajsky)\
**Post date:** [May 29, 2019, 4:49pm UTC](https://forum.katalon.com/t/how-to-create-a-test-that-saves-the-token-of-my-api/12391/12 "2019-05-29T16:49:08Z")

</div>

i edited code i posted  
and it should be like this:

```auto
def token = response.getHeaderFields().tokenCookie 
myCookie = new Cookie("token", token)
def newResponse = WS.sendRequest(findTestObject(ro.getObjectId(),[('COOKIE'): myCookie]))

```

---

<div class="post-metadata">

**Author:** ![thuyennguyen](https://avatars.discourse-cdn.com/v4/letter/t/e79b87/32.png) [@thuyennguyen](https://forum.katalon.com/u/thuyennguyen)\
**Post date:** [May 30, 2019, 10:05am UTC](https://forum.katalon.com/t/how-to-create-a-test-that-saves-the-token-of-my-api/12391/13 "2019-05-30T10:05:21Z")

</div>

Run the access API -\> get the body of the access API + with the other APIs, set the token as global variable -\> run the other APIs

response1 = WS.sendRequest(findTestObject(‘A’))

def slurper = new groovy.json.JsonSlurper()

def result = slurper.parseText(response1.getResponseBodyContent())

response2 = WS.sendRequest(findTestObject(‘B’, [(‘token’) : result.data.accessToken]))

---

<div class="post-metadata">

**Author:** ![Andrej\_Podhajsky](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/andrej_podhajsky/32/1055_2.png) [@Andrej\_Podhajsky](https://forum.katalon.com/u/Andrej_Podhajsky)\
**Post date:** [May 30, 2019, 12:46pm UTC](https://forum.katalon.com/t/how-to-create-a-test-that-saves-the-token-of-my-api/12391/14 "2019-05-30T12:46:52Z")

</div>

and do you have token variable in B TO?

---

<div class="post-metadata">

**Author:** ![thuyennguyen](https://avatars.discourse-cdn.com/v4/letter/t/e79b87/32.png) [@thuyennguyen](https://forum.katalon.com/u/thuyennguyen)\
**Post date:** [May 30, 2019, 6:56pm UTC](https://forum.katalon.com/t/how-to-create-a-test-that-saves-the-token-of-my-api/12391/15 "2019-05-30T18:56:27Z")

</div>

A is Login API to get token  
B is example of the other APIs  
=\> (‘token’) is a global variable that you just set one time and use for all the APIs

---

<div class="post-metadata">

**Author:** ![wale\_Goold](https://avatars.discourse-cdn.com/v4/letter/w/b38774/32.png) [@wale\_Goold](https://forum.katalon.com/u/wale_Goold)\
**Post date:** [June 4, 2019, 9:21pm UTC](https://forum.katalon.com/t/how-to-create-a-test-that-saves-the-token-of-my-api/12391/16 "2019-06-04T21:21:41Z")

</div>

Thanks guys
