Katalon Analytics (beta) – Bringing new insights into test reports. Learn more!

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

Comments

  • 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)
  • Hopefully, going thru this sample test case you will find the answer

    import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
    import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
    import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
    import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
    import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
    import com.kms.katalon.core.checkpoint.CheckpointFactory as CheckpointFactory
    import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as MobileBuiltInKeywords
    import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
    import com.kms.katalon.core.model.FailureHandling as FailureHandling
    import com.kms.katalon.core.testcase.TestCase as TestCase
    import com.kms.katalon.core.testcase.TestCaseFactory as TestCaseFactory
    import com.kms.katalon.core.testdata.TestData as TestData
    import com.kms.katalon.core.testdata.TestDataFactory as TestDataFactory
    import com.kms.katalon.core.testobject.ConditionType
    import com.kms.katalon.core.testobject.ObjectRepository as ObjectRepository
    import com.kms.katalon.core.testobject.RequestObject
    import com.kms.katalon.core.testobject.ResponseObject
    import com.kms.katalon.core.testobject.TestObject as TestObject
    import com.kms.katalon.core.testobject.TestObjectProperty
    import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WSBuiltInKeywords
    import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
    import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUiBuiltInKeywords
    import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
    import internal.GlobalVariable as GlobalVariable

    // FIRST STEP
    // load test request object which will do the login step and retrieve the token for later usages
    RequestObject 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 runtime
    body = 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 body
    getTokenTestObject.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 object
    def tokenObject = new groovy.json.JsonSlurper().parseText(responseTokenObject.getResponseText())

    // get the retrieved token
    token = tokenObject.accessToken

    // SECOND STEP
    // load test request object which will use token above in Authorization
    RequestObject getUserInfoTestObject = findTestObject('Auth/Get User Info')

    // if getUserInfoTestObject HTTP headers have no Authorization item
    getUserInfoTestObject.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)

     
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!