Web Services Builder


This is a companion discussion topic for the original entry at https://docs.katalon.com/katalon-studio/docs/web-services-builder.html

Thank you, I am new to Katalon and scripting. This is working good for me so far. I have one problem though. I am using a data file and it is not applying the Restparameters as expected. Could you please advise? Thank you

import com.kms.katalon.core.testobject.ConditionType as ConditionType
import com.kms.katalon.core.testobject.RestRequestObjectBuilder
import com.kms.katalon.core.testobject.TestObjectProperty as TestObjectProperty
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS

//Create a new GET object using builder’
for (def row=1; row<=findTestData(“GETRequestBuilder”).getRowNumbers(); row++) {

def builder = new RestRequestObjectBuilder()
def requestObject = builder
.withRestRequestMethod(“GET”)
.withRestUrl("${GlobalVariable.RestURL}"+ findTestData(‘GETRequestBuilder’).getValue(‘EndPoint’, row))
.withRestParameters([
new TestObjectProperty(“PageNumber”, ConditionType.EQUALS, findTestData(‘GETRequestBuilder’).getValue(‘PageNumber’, row)),
new TestObjectProperty(“PageSize”, ConditionType.EQUALS, findTestData(‘GETRequestBuilder’).getValue(‘PageSize’, row)),
])
.withHttpHeaders([
new TestObjectProperty(“Content-Type”, ConditionType.EQUALS, “application/json”),
new TestObjectProperty(“Authorization”, ConditionType.EQUALS, GlobalVariable.token)
])
.build()

//‘Send a request’
def response = WS.sendRequest(requestObject)

//Verify status code
WS.verifyResponseStatusCode(response, 200)

//Write response to the Console
KeywordUtil.logInfo(response.responseBodyContent)
}

“log” : {
“version” : “1.2”,
“pages” : [ ],
“entries” : [ {
“startedDateTime” : “2020-11-13T14:15:42.945Z”,
“request” : {
“method” : “GET”,
“url” : “https://portal.xxxxx.xxxx.com/backend/api/v2018-07-17/antenna-packages”,
“httpVersion” : “”,
“cookies” : [ ],
“headers” : [ {
“name” : “Content-Type”,
“value” : “application/json”

Data files looks as follows:
image

Hi @santie.laing,
Thank you for your report. We will fix this in upcoming releases.

1 Like

Thank you @huynguyen
Is there another way to get the same result in the meantime?
Regards

Hi @santie.laing,
As a workaround, you can append the parameters directly into your URL.

Hi @huynguyen. That is what I did in the end. It works fine for what I need it for. Thank you for your reply. :slight_smile:

Good day,
Would you mind to give me advice please?
I am using a global variable in the rest body, but I am doing something wrong or it’s not escaping as expected. I am referring to - String body = ‘{“name”:"${GlobalVariable.newCompanyName}"}’ I’ve tried to use single quotes around the variable as well.
It works fine if I use a hard coded value in double quotes in stead of the variable. e.g. {“name”:“Santie14”}.
Thank you! :slight_smile:

//Set date variable
def todaysDate = new Date()
def todaysDateFormatted = todaysDate.format(‘ddMMyy-HH:mm:ss.SSS’)

//Set new company name as ‘New Company + today’s date and time)
value0 = ‘Auto New Company’ + todaysDateFormatted
println(’…New Company Name is:’ + value0)

//Store new company name in Global Variable
GlobalVariable.newCompanyName = value0
println(GlobalVariable.newCompanyName)
String body = '{“name”:"${GlobalVariable.newCompanyName}"}'
println(body)

//Create a new POST object, using builder to create a new company with this unique name
def builder = new RestRequestObjectBuilder()
def requestObject = builder
.withRestRequestMethod(“POST”)
.withRestUrl("${GlobalVariable.RestURL}"+ “companies”)
.withHttpHeaders([
new TestObjectProperty(“Content-Type”, ConditionType.EQUALS, “application/json”),
new TestObjectProperty(“Authorization”, ConditionType.EQUALS, GlobalVariable.token)
])
.withTextBodyContent(body)
.build()

//‘Send the request’
def response = WS.sendRequest(requestObject)

//Verify status code
WS.verifyResponseStatusCode(response, 200)

//Parse the response
def jsonSlurper = new JsonSlurper()
def result = jsonSlurper.parseText(response.getResponseBodyContent())
def valueId = result.id
def valueName = result.name
println(’…Id extracted is : ’ +valueId)
println(’…Name extracted is : ’ +valueName)

Hi @santie.laing,
Please try `String body = “{“name”:”${GlobalVariable.newCompanyName}"}"

Hi @huynguyen
Thank you for your prompt reply. I adjusted it a bit and it worked for me. Tx again. :slight_smile:
String body = “{“name”:”${GlobalVariable.newCompanyName}"}"
Kind regards

@santie.laing,
Glad to hear that. I should have said that you ought to put some escape characters before using my answer, but you did it without further help. :wink:

1 Like