Table of contents
1.
Introduction
2.
Creating a Web Service Object
2.1.
REST GET Object
2.2.
REST POST Object
3.
Frequently Asked Questions
3.1.
What is the drawback of programmatically creating a web service object?
3.2.
How to create Web Service Objects that can be reused and customised?
3.3.
What are the two classes used to create request objects?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Create Web Service Object in Script Mode in Katalon

Author Yashesvinee V
0 upvote

Introduction

Hi there! 

Do you know what makes Katalon Studio one of the most popular tools for automated testing? Katalon offers dual scripting interfaces for users with different programming skills - a simple user interface and a Script view. Creating a web service object using scripts is most preferred by users proficient in programming and Katalon but is easy even for beginners.

The article Introduction to Web Service Objects in Katalon shows how to create a Web Service Object using the user interface.

Create Web Service Object in Script Mode in Katalon

Creating a Web Service Object

Katalon Studio allows the creation of web service objects for REST and SOAP services. They can be manipulated even within test cases in the Script View. Here are some examples.

REST GET Object

Step 1: Create a new Test case and switch to the Script View.

New test case in script view

Step 2: Add the following code in the Editor.

import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.testobject.TestObjectProperty
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import java.util.*

// Setting HTTP header properties
List<TestObjectProperty> httpHeaderProperties = new ArrayList<>()
httpHeaderProperties.add(new TestObjectProperty("Content-Type", ConditionType.EQUALS, 'application/json'))
httpHeaderProperties.add(new TestObjectProperty("Authorization", ConditionType.EQUALS, 'None'))
RequestObject request = new RequestObject()
request.setServiceType('REST')
request.setHttpHeaderProperties(httpHeaderProperties)
request.setRestUrl('https://reqres.in/api/users?page=2')
request.setRestRequestMethod('GET')

// Sending request
response = WS.sendRequest(request)

// Verifying response
WS.verifyResponseStatusCode(response, 200)

The above code creates a RESTful web service object. All HTTP header properties are stored in a list. A request is created using the RequestObject class with the URL and request method values. The request is sent, and the response is verified.

Step 3: Click on the Run icon to execute the script.

Run test case

Execution result

REST POST Object

For the POST method in REST, we require a request body. The request body is stored in a HashMap object. It is n converted into a JSON Object and then to a String. The resultant String is stored as a HttpTextBodyContent object for the POST request.

import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.HttpBodyContent
import com.kms.katalon.core.testobject.impl.HttpTextBodyContent
import com.kms.katalon.core.testobject.TestObjectProperty
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import java.util.*

HashMap<String, Object> requestBody = new HashMap<>();

requestBody.put('username', 'Marcus')
requestBody.put('job', 'Team Lead')

// Map to JSON
def jsonrequestBody = new groovy.json.JsonBuilder()
jsonrequestBody fields: requestBody

// JSON to String 
String content =  jsonrequestBody.toString()
// Create body content property
HttpTextBodyContent httpBody = new HttpTextBodyContent(content);

// HTTP Header
List<TestObjectProperty> httpHeader = new ArrayList<>()
httpHeader.add(new TestObjectProperty("Content-Type", ConditionType.EQUALS, 'application/json'))

// Request
RequestObject reqOBJ = new RequestObject()
reqOBJ.setBodyContent(httpBody)
reqOBJ.setServiceType('REST')
reqOBJ.setRestRequestMethod('POST')
reqOBJ.setHttpHeaderProperties(httpHeader)
reqOBJ.setRestUrl('https://reqres.in/api/users')

resp = WS.sendRequest(reqOBJ)

// Verify Response Status
WS.verifyResponseStatusCode(resp, 201)

Frequently Asked Questions

What is the drawback of programmatically creating a web service object?

Web service objects created by scripts do not offer reusability, i.e., these requests are for one-time use only. This is because the URL, HTTP header method and other properties are written within the script. The script will have to be changed for different URLs and properties.

How to create Web Service Objects that can be reused and customised?

For reusability and customisability, we can create a Custom Keyword that builds a Web Service Object as specified and call it in the test cases.

What are the two classes used to create request objects?

The RequestObject and RestRequestObjectBuilder classes can be used to create request objects.

Conclusion

This blog explains how to create a Web Service Object in Script mode in Katalon Studio. It explains how to send a REST GET and POST request in the Script mode. Check out our articles on Generate Test Steps in Manual and Script View, Manage Test Projects in Katalon and Manage Mobile Test Objects 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