Introduction
Hey there!
Are you an experienced user of Katalon? Then you might want to know how to create RESTful requests manually. RESTful requests can be created using the RequestObject class or the RestRequestObjectBuilder class in the Script view of Katalon. This blog will focus on the RestRequestObjectBuilder class's functions and some examples to learn its use.
The RestRequestObjectBuilder class
The RestRequestObjectBuilder class belongs to the com.kms.katalon.core.testobject package. It returns a RequestObject object. Let us take a look at the different functions this class has to offer.
Functions
Examples
Now that we have learnt about the functions of the RestRequestObjectBuilder class, let's see an example of creating a GET and POST request.
GET request
Step 1: Create a new test case and switch to Script view.
Step 2: Add the code and click on Run.
import com.kms.katalon.core.testobject.RestRequestObjectBuilder
import com.kms.katalon.core.testobject.TestObjectProperty as TestObjectProperty
import com.kms.katalon.core.testobject.ConditionType as ConditionType
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
def ObjectBuilder = new RestRequestObjectBuilder()
def requestObject = ObjectBuilder
.withRestRequestMethod("GET")
.withRestUrl("https://reqres.in/api/users?page=2")
.withRestParameters([new TestObjectProperty("id", ConditionType.EQUALS, "10")])
.withHttpHeaders([new TestObjectProperty("Content-Type", ConditionType.EQUALS, "application/json")])
.build()
def response = WS.sendRequest(requestObject)
WS.verifyResponseStatusCode(response, 200)

POST request
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.testobject.UrlEncodedBodyParameter
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
def ObjectBuilder = new RestRequestObjectBuilder()
'Create a new POST object using builder'
def requestObject = ObjectBuilder
.withRestRequestMethod("POST")
.withRestUrl("https://reqres.in/api/users")
.withHttpHeaders([
new TestObjectProperty("Content-Type", ConditionType.EQUALS, "application/x-www-form-urlencoded")
])
.withUrlEncodedBodyContent([
new UrlEncodedBodyParameter("name", "Marcus"),
new UrlEncodedBodyParameter("job", "marcus@2022"),
])
.build()
def response = WS.sendRequest(requestObject)
WS.verifyResponseStatusCode(response, 201)

A request with NTLM Authorization
import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.testobject.RestRequestObjectBuilder
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.testobject.authorization.NTLMAuthorization
NTLMAuthorization auth = new NTLMAuthorization("username", "password", "", "")
RequestObject request = new RestRequestObjectBuilder()
.withRestUrl("URL")
.withRequestAuthorization(auth)
.withRestRequestMethod("GET")
.build()
ResponseObject response = WS.sendRequest(request)