Table of contents
1.
Introduction
2.
The RestRequestObjectBuilder class 
2.1.
Functions
2.2.
Examples
2.2.1.
GET request
2.2.2.
POST request
2.2.3.
A request with NTLM Authorization
3.
Frequently Asked Questions
3.1.
What is NTLM?
3.2.
What is the advantage of using RestRequestObjectBuilder?
3.3.
Name some exceptions that may be thrown while using the RestRequestObjectBuilder class.
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Web Services Builder in Katalon Studio

Author Yashesvinee V
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

Web Services Builder in Katalon Studio

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

Functions of the RestRequestObjectBuilder class

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)

 

GET Request execution result

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)

 

POST Request execution result

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)

Frequently Asked Questions

What is NTLM?

NTLM is a protocol used for authenticating users accessing a resource in Windows. It only authenticates the connections but not the requests. Therefore, the inputs are directly sent to the request and not to the HTTP header.

What is the advantage of using RestRequestObjectBuilder?

RestRequestObjectBuilder provides additional methods like MultipartFormBodyContent and FileBodyContent for sending a POST request. It is much easier to use compared to using the RequestObject class directly.

Name some exceptions that may be thrown while using the RestRequestObjectBuilder class.

UnsupportedEncodingException is thrown when an unsupported character encoding scheme is adopted. FileNotFoundException and IllegalArgumentException are some other exceptions. 

Conclusion

This blog explains the RestRequestObjectBuilder class. It explains how the class is used as a web service builder by describing its functions with examples. Check out our articles on Creating Web service objects in Script mode Generate Test Steps in Manual and Script View, Sample API tests project in Katalon and Search and Call Test cases in Katalon.

Explore our Library on Coding Ninjas Studio to gain knowledge on Data Structures and Algorithms, Machine Learning, Deep Learning, Cloud Computing and many more! Test your coding skills by solving our test series and participating in the contests hosted on Coding Ninjas Studio! Looking for questions from tech giants like Amazon, Microsoft, Uber, etc.? Look at the problems, interview experiences, and interview bundle for placement preparations. Upvote our blogs if you find them insightful and engaging! Happy Coding!

Thank you
Live masterclass