How to print the response of the REST API call


#1

Hi All,

I am trying the print the API response for the Rest API call which I have made.

Also I am trying to fetch some property values from this call to use them in subsequent calls as input.

Is there a way to achieve this in Katalon?

Any help is much appreciated.

Thanks,
Sampath


#2

groovy.json.JsonOutput class would help:

See


#3

Thanks alot this helped me to fetch the data.

Can you please also guide me how can I pass this fetched data as variable to the next API call?


#4

I do not see how I can help you.

Please, write your some code first. The code may be wrong, that’s OK.

Show your code to us.

The code would tell us what you want to do, how much you are trained for Groovy programming, and what you need to learn more.


#5

sendOtpResponse = WS.sendRequest(findTestObject(‘sendOTP’))

sendOtpResponseText = sendOtpResponse.getResponseText()

JsonSlurper slurper1 = new JsonSlurper()

Map parsedJson1 = slurper1.parseText(sendOtpResponseText)

String auth_Id = parsedJson1.auth_id

println(auth_Id)

String auth_id = “AUTH-BV57PV63UB6L”

getOtpResponse = WS.sendRequest(findTestObject(‘getOtp’, [(‘auth_id’) : auth_id]))

getOtpResponseText = getOtpResponse.getResponseText()\

JsonSlurper slurper = new JsonSlurper()

Map parsedJson = slurper1.parseText(getOtpResponseText)

String otp = parsedJson.otp_code

println(otp)

This is the code I have written but I am unable to pass the auth_id from the previous sendOtpcall to getOtpcall
Need to know where I am going wrong


#6

I inserted some debug-print statements as follows. Please run it. and show us what messages you see in the console.

import groovy.json.JsonOutput
import com.kms.katalon.core.testobject.TestObject

sendOtpResponse = WS.sendRequest(findTestObject(‘sendOTP’))
sendOtpResponseText = sendOtpResponse.getResponseText()
JsonSlurper slurper1 = new JsonSlurper()

//Map parsedJson1 = slurper1.parseText(sendOtpResponseText)
def parsedJson1 = slurper1.parseText(sendOtpResponseText)
println "#1 parsedJson1=${JsonOutput.prettyPrint(parsedJson1)}"

String auth_Id = parsedJson1.auth_id

println("#2 auth_Id=${auth_id}")

String auth_id = “AUTH-BV57PV63UB6L”

TestObject to = findTestObject(‘getOtp’, [(‘auth_id’) : auth_id])
List<TestObjectProperty> list = to.getActiveProperties()
for (TestObjectProperty top : list) {
	println "${top.getName()} ${top.getValue()}"
}

getOtpResponse = WS.sendRequest(to)
getOtpResponseText = getOtpResponse.getResponseText()
JsonSlurper slurper = new JsonSlurper()

//Map parsedJson = slurper1.parseText(getOtpResponseText)
def parsedJson = slurper1.parseText(getOtpResponseText)
println "#4 parseJson=${JsonOutput.prettyPrint(parsedJson)}"

String otp = parsedJson.otp_code
println(otp)