How to use a Response value as input into another API request

Scenario: API One generates a “token” value in the response. How can this be captured and used as input to into API Two?

API Two has the “token” value in the HTTP Header and not as a parameter of the GET request

TIA

Give this a try:

hi,

as this nice slurper
// Parse the response
def restResponse = ‘{“token”: “ewrerydjgkfgijrtupk,nyouktu”,“tokenValidityTime”:“300”,“signInStatus”: “LoginSuccessfull”,“accounts”: [{“accNumber”: “6000112216066021”,“balance”: “0”,“routingNumber”: “122106316”}],“profileInformation”: {“first_name”: “Apple”,“last_name”: “Test”,“sysDate”: “2020-01-30T15:46:40.969Z”}}’
def list = new JsonSlurper().parseText( restResponse )
// Print them out to make sure
list.each { println it }
accounts=[{accNumber=6000112216066021, balance=0, routingNumber=122106316}]
profileInformation={first_name=Apple, last_name=Test, sysDate=2020-01-30T15:46:40.969Z}
signInStatus=LoginSuccessfull
token=ewrerydjgkfgijrtupk,nyouktu
tokenValidityTime=300

No, this isn’t what I was looking for as I don’t have that type of programming background to even make heads or tails out of that info…

Is there no Web Service Keyword (maybe like Get Element Property Value) that can be set as a parameter into the HTTP Header of the second API request?

1 Like

hello,

sometimes (usual) testers should have some coding skills too :slight_smile:
// Parse the response
def restResponse = ‘{“token”: “ewrerydjgkfgijrtupk,nyouktu”,“tokenValidityTime”:“300”,“signInStatus”: “LoginSuccessfull”,“accounts”: [{“accNumber”: “6000112216066021”,“balance”: “0”,“routingNumber”: “122106316”}],“profileInformation”: {“first_name”: “Apple”,“last_name”: “Test”,“sysDate”: “2020-01-30T15:46:40.969Z”}}’
def list = new JsonSlurper().parseText( restResponse )
// Print them out to make sure
list.each { println it }
String searchToken = “”
for (String s : list){
if (s.startsWith(“token=”)){
searchToken = s
break;
}
}

println searchToken
String[] parts = searchToken.split("=");
String part1 = parts[0]; 
String part2 = parts[1]; 
println part1 
println part2 //here is token ewrerydjgkfgijrtupk,nyouktu

Is there not a Web Service Keyword for returning the Response values from the API call?

hello,

check this chain

Ok, I tried your 1st suggestion here is what I placed in my Script

def slurper = new groovy.json.JsonSlurper()
def result = slurper.parseText(response.getResponseBodyContent())
println ("… response: "+result)

and the script failed with the following error:

2020-02-07 12:49:45.990 DEBUG testcase.Sign In - 3: slurper = new groovy.json.JsonSlurper()
2020-02-07 12:49:45.990 DEBUG testcase.Sign In - 4: result = slurper.parseText(response.getResponseBodyContent())
2020-02-07 12:49:46.008 ERROR c.k.katalon.core.main.TestCaseExecutor - :x: Test Cases/SignIn/Sign In FAILED.
Reason:
groovy.lang.MissingPropertyException: No such property: response for class: Script1580162573814
at Sign In.run(Sign In:24)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1581104977982.run(TempTestCase1581104977982.groovy:23)

Do I need to include some other file in my script? or is it something else?

hi,

and this for slurper import groovy.json.JsonSlurper
it’s there
very simple with Gson library

import com.google.gson.Gson;

def usertoken = result.body.userToken
Gson gson = new Gson();
String json = gson.toJson(usertoken);