Passing values into API calls

Hi,

I need to be able to pass values between API calls.

For example, if I have 2 API calls, getToken and getUsers and from getToken I obtain token that I need to pass to getUsers call as a part of HTTP Header.

 

Something like “Authorization”: “Bearer ${GlobalVariable.gToken}”

 

Thanks,
R

Hopefully, going thru this sample test case you will find the answer

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpointimport static com.kms.katalon.core.testcase.TestCaseFactory.findTestCaseimport static com.kms.katalon.core.testdata.TestDataFactory.findTestDataimport static com.kms.katalon.core.testobject.ObjectRepository.findTestObjectimport com.kms.katalon.core.checkpoint.Checkpoint as Checkpointimport com.kms.katalon.core.checkpoint.CheckpointFactory as CheckpointFactoryimport com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as MobileBuiltInKeywordsimport com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobileimport com.kms.katalon.core.model.FailureHandling as FailureHandlingimport com.kms.katalon.core.testcase.TestCase as TestCaseimport com.kms.katalon.core.testcase.TestCaseFactory as TestCaseFactoryimport com.kms.katalon.core.testdata.TestData as TestDataimport com.kms.katalon.core.testdata.TestDataFactory as TestDataFactoryimport com.kms.katalon.core.testobject.ConditionTypeimport com.kms.katalon.core.testobject.ObjectRepository as ObjectRepositoryimport com.kms.katalon.core.testobject.RequestObjectimport com.kms.katalon.core.testobject.ResponseObjectimport com.kms.katalon.core.testobject.TestObject as TestObjectimport com.kms.katalon.core.testobject.TestObjectPropertyimport com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WSBuiltInKeywordsimport com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WSimport com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUiBuiltInKeywordsimport com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUIimport internal.GlobalVariable as GlobalVariable// FIRST STEP// load test request object which will do the login step and retrieve the token for later usagesRequestObject getTokenTestObject = findTestObject('Auth/Get User Token')// current value: { username: '_username_', password: '_password_'}// suppose that _username_ and _password_ are the invalid values and you want to change on runtimebody = getTokenTestObject.getHttpBody().replace("_username_", "valid username").replace("_password_", "valid password")// You might use Test Case variable or Global variable to store the user credentials// update back the HTTP request bodygetTokenTestObject.setHttpBody(body)ResponseObject responseTokenObject = WS.sendRequest(getTokenTestObject)// suppose that the responseTokenObject.getResponseText() is { accessToken: "xxx", expiredTime: "yyy" }// use groovy.json.JsonSlurper to parse the text into objectdef tokenObject = new groovy.json.JsonSlurper().parseText(responseTokenObject.getResponseText())// get the retrieved tokentoken = tokenObject.accessToken// SECOND STEP// load test request object which will use token above in AuthorizationRequestObject getUserInfoTestObject = findTestObject('Auth/Get User Info')// if getUserInfoTestObject HTTP headers have no Authorization itemgetUserInfoTestObject.getHttpHeaderProperties().add(new TestObjectProperty("Authorization", ConditionType.EQUALS, "Bearer " + token))// Otherwise, you need to go through the getUserInfoTestObject.getHttpHeaderProperties() and replace the value of Authorization header//for (TestObjectProperty header: getUserInfoTestObject.getHttpHeaderProperties()) {//	if ("Authorization".equals(header.getName())) {//		header.setValue("Bearer " + token);//		break;//	}//}WS.sendRequest(getUserInfoTestObject)
1 Like

Hi Rasko,
first of all, you have to save your token from first API call into a variable.
Then, please try following code:

TestObjectProperty prop = new TestObjectProperty()
prop.setName("Authorization")
prop.setValue("Bearer " + yourToken)

List<TestObjectProperty> headers = new ArrayList<>()
headers.add(prop)

RequestObject reqObj = new RequestObject()
reqObj.setHttpHeaderProperties(headers)
reqObj.setRestRequestMethod('POST')
reqObj.setRestUrl('https://www.yourserver.com/api/endpoint')

def getResponse = WSBuildInKeywords.sendRequest(reqObj)

@Marek. I have used the same method that you have suggested, however the tool is thrown with the following error. Any thoughts ?

Reason:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
file:/C:/Users/spullabhotla/Katalon%20Studio/BctiTestProject/Scripts/WSAT%20Test%20Cases/external%20(SendGrid)/LaunchCampaignTest/Script1549494438447.groovy: 20: unable to resolve class TestObjectProperty
@ line 20, column 20.
TestObjectProperty prop = new TestObjectProperty()
^

file:/C:/Users/spullabhotla/Katalon%20Studio/BctiTestProject/Scripts/WSAT%20Test%20Cases/external%20(SendGrid)/LaunchCampaignTest/Script1549494438447.groovy: 20: unable to resolve class TestObjectProperty
@ line 20, column 27.
TestObjectProperty prop = new TestObjectProperty()
^

file:/C:/Users/spullabhotla/Katalon%20Studio/BctiTestProject/Scripts/WSAT%20Test%20Cases/external%20(SendGrid)/LaunchCampaignTest/Script1549494438447.groovy: 24: unable to resolve class TestObjectProperty
@ line 24, column 6.
List headers = new ArrayList<>()
^

file:/C:/Users/spullabhotla/Katalon%20Studio/BctiTestProject/Scripts/WSAT%20Test%20Cases/external%20(SendGrid)/LaunchCampaignTest/Script1549494438447.groovy: 27: unable to resolve class RequestObject
@ line 27, column 15.
RequestObject reqObj = new RequestObject()
^

file:/C:/Users/spullabhotla/Katalon%20Studio/BctiTestProject/Scripts/WSAT%20Test%20Cases/external%20(SendGrid)/LaunchCampaignTest/Script1549494438447.groovy: 27: unable to resolve class RequestObject
@ line 27, column 24.
RequestObject reqObj = new RequestObject()