Table of contents
1.
Introduction
2.
Creating a REST request through UI
2.1.
Request 
2.1.1.
Request method
2.1.2.
Request URL
2.1.3.
Query Parameters
2.1.4.
Request Body
2.1.5.
Authentication
2.2.
Response View
3.
Creating REST requests manually
3.1.
Request Object 
3.2.
Response Object
4.
Frequently Asked Questions
4.1.
Name the parts of a RESTful Web Service object where variables can be used.
4.2.
What is the advantage of using RestRequestObjectBuilder?
4.3.
How is the request body specified for POST requests manually?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

REST Request in Katalon

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

Introduction

Hi there!

Did you know that before REST was developed, SOAP was used to integrate APIs? As SOAP was known to be complex to build and use, REST was created as a standard to facilitate communication between servers. In this blog, we shall discuss creating REST requests in Katalon Studio.

REST Request in Katalon

Creating a REST request through UI

Responses are received for every request sent to the API server. This section explains how to create a RESTful request using Katalon Studio's UI. RESTful requests in Katalon can be created by adding a Web service request to a New or Existing test case. The options are displayed by clicking on the plus icon at the top.

New REST request

Request 

Here is how you can create a new Web service request.

Step 1: After creating a new project, create a new web service request by going to Test Explorer -> Object Repository -> New -> Web Service Request.

New Web Service Request

Step 2: Type in the necessary details and click OK.

Name New Request

Step 3: Enter the request method, URL, and Authorisation.

Request method

Katalon Studio provides nine request methods that indicate the action to be carried out by request. They are:

  • GET - Search for a resource.
     
  • POST - Create and add a new resource.
     
  • PUT - Update an existing resource.
     
  • DELETE - Delete a particular resource.
     
  • PATCH - Partially update a resource.
     
  • HEAD - Request for headers.
     
  • CONNECT - Initiate two-way communication with a resource.
     
  • TRACE - Trace a message to the target resource.
     
  • OPTIONS - Request communication options.
     

REST request methods

The default method for all new requests is GET. We can also create a custom method by adding it in the Project > Settings > Test Design > Web Service > Custom Method window.

Request URL

The URL must be a service endpoint that receives the request. URLs can contain query parameters within their body, as shown below.

Request URL

Query Parameters

They are embedded within the request object. They are used to find a resource specifically.

Request Body

It contains the information to be sent along with the request, especially for POSTPUT and PATCH. There are various options to set the request body.

HTTP Request Body

Authentication

Authenticating and authorising requests is essential as it verifies the client's identity. An appropriate response is sent if the user authentication is successful. Katalon Studio displays five types of auth.

  • Bearer - It requires security tokens called bearer tokens for authentication.
  • Basic - It involves a username and password for verification.
  • OAuth 1.0 
  • OAuth 2.0
  • NTLM

Request Authentication

Response View

The key information derived from a Response message is the Status code, Elapsed time and size of the response. 

HTTP Response view

The response body contains the actual response received from the API server. We can view the response body in JSON, XML, HTML or JavaScript format. 

Creating REST requests manually

Now that we have seen how to create REST requests using the UI. Let's see how to do this manually if you happen to be an experienced user of Katalon.

Request Object 

Handling API requests manually involves the use of the RequestObject and ResponseObject classes. To create a RequestObject instance, use setter methods to set the request information.

  • setRestUrl() sets the request URL.
  • setRestRequestMethod() sets the request method.
  • setHttpHeaderProperties sets the request header properties.
     

Alternatively, we can also use the RestRequestObjectBuilder class to return an instance of the RequestObject. This class uses the build() method along with the following.

  • withRestUrl()
  • withHttpHeaders()
  • withRestRequestMethod()
     

Here is an example of sending a GET request using the RequestObject class.

import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.testobject.RestRequestObjectBuilder
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.impl.HttpTextBodyContent
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS

public class SampleRequest 
{
	String endpointURL = "http://universities.hipolabs.com/search?country=China"
	String reqMethod = "GET"
	String AuthHeader = "RequestAuthentication"

	TestObjectProperty h1 = new TestObjectProperty("Authorization", ConditionType.EQUALS, AuthHeader)
	TestObjectProperty h2 = new TestObjectProperty("Content-Type", ConditionType.EQUALS, "application/json")
	ArrayList AllHeaders = Arrays.asList(h1, h2)

	public ResponseObject APIRequest1() 
	{
		RequestObject req = new RequestObject("objectId")
		req.setRestUrl(endpointURL)
		req.setHttpHeaderProperties(AlltHeaders)
		req.setRestRequestMethod(reqMethod)

		ResponseObject resp = WS.sendRequest(req)
		return resp
	}
}

The below example shows the use of RestRequestObjectBuilder class for the same request.

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

public class SampleRequest 
{
	String endpointURL = "http://universities.hipolabs.com/search?country=China"
	String reqMethod = "GET"
	String AuthHeader = "RequestAuthentication"

	TestObjectProperty h1 = new TestObjectProperty("Authorization", ConditionType.EQUALS, AuthHeader)
	TestObjectProperty h2 = new TestObjectProperty("Content-Type", ConditionType.EQUALS, "application/json")
	ArrayList AllHeaders = Arrays.asList(h1, h2)

	public ResponseObject APIRequest2() 
	{
		RequestObject req = new RestRequestObjectBuilder()
		.withRestUrl(endpointURL)
		.withHttpHeaders(AllHeaders)
		.withRestRequestMethod(reqMethod)
		.build()

		ResponseObject resp = WS.sendRequest(req)
		return resp
	}
}

Response Object

We receive an instance of the ResponseObject class after sending a RequestObject instance to the API server. All information from the Response object can be retrieved using the following methods:

  • getStatusCode()
  • getResponseText()
  • getResponseBodySize()
  • getResponseHeaderSize()
  • getWaitingTime()

Frequently Asked Questions

Name the parts of a RESTful Web Service object where variables can be used.

Variables can be used and called in the request URL, query parameters, HTTP header, HTTP body and verification. They can be declared in the Variables tab.

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.

How is the request body specified for POST requests manually?

The request body is specified within setBodyContent() method while using the RequestObject class. The withTextBodyContent() method is used while using the RestRequestObjectBuilder class.

Conclusion

This blog explains how to create REST requests in Katalon Studio using the UI and manually. It briefly highlights all the information as part of the request and response messages. Check out our articles on 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