DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Building REST API Backend Easily With Ballerina Language
  • Composite Requests in Salesforce Are a Great Idea
  • CouchDB REST API for Document CRUD Operations — Examples With Postman
  • REST APIs: Simplicity, Flexibility, and Adoption

Trending

  • The Ultimate Guide to Code Formatting: Prettier vs ESLint vs Biome
  • How Large Tech Companies Architect Resilient Systems for Millions of Users
  • Simplifying Multi-LLM Integration With KubeMQ
  • Designing for Sustainability: The Rise of Green Software
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Create REST API Requests Manually With Katalon Studio

Create REST API Requests Manually With Katalon Studio

While Katalon Studio allows you to automate testing, its libraries of support methods can be used manually for API requests.

By 
Marek Melocik user avatar
Marek Melocik
·
Updated Sep. 18, 18 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
12.6K Views

Join the DZone community and get the full member experience.

Join For Free

Katalon Studio offers great UI support for creating REST API requests, but if you are an advanced Katalon user, you can do it manually and benefit from the large library of Katalon support methods for API requests. This tutorial will show how to create REST API requests manually and handle responses to make your code robust and effective.

Requirements

You should be familiar with Katalon Studio and know the basics of Java/Groovy.

RequestObject and ResponseObject

These are the two main classes for handling API requests. I am sure you have already figured out that the RequestObject class represents an API request and the WSBuiltInKeywords.sendRequest method returns ResponseObject. Now, let’s create a REST API request as an example (of course, you can do the same with SOAP API requests). You first need to have information for a request, including

  • URL
  • Request method (GET, POST, PUT, DELETE…)
  • Request headers (Authorization, Content-Type…)
  • Request body (for POST request)
  • REST request parameters

Next, you create an object from RequestObject using either one of the following ways:

  • A new RequestObject instance directly and set its information using setter methods such as setRestUrl() and setRestRequestMethod().
  • Use RestRequestObjectBuilder, a useful helper class offered by Katalon Studio version 5.4 and later.
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.testobject.TestObjectProperty
import com.kms.katalon.core.testobject.impl.HttpTextBodyContent
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS

public class SampleRequestObject {

 String endpoint = "https://www.katalon.com"
 String requestMethod = "GET"
 String authHeader = "whateverYouNeedForAuthentication"

 TestObjectProperty header1 = new TestObjectProperty("Authorization", ConditionType.EQUALS, authHeader)
 TestObjectProperty header2 = new TestObjectProperty("Content-Type", ConditionType.EQUALS, "application/json")
 TestObjectProperty header3 = new TestObjectProperty("Accept", ConditionType.EQUALS, "application/json")
 ArrayList defaultHeaders = Arrays.asList(header1, header2, header3)

 /**
  * GET requests
  * @return
  */
 public ResponseObject buildApiRequest1() {
  RequestObject ro = new RequestObject("objectId")
  ro.setRestUrl(endpoint)
  ro.setHttpHeaderProperties(defaultHeaders)
  ro.setRestRequestMethod(requestMethod)

  ResponseObject respObj = WS.sendRequest(ro)
  return respObj
 }

 public ResponseObject buildApiRequest2() {
  RequestObject ro = new RestRequestObjectBuilder()
   .withRestUrl(endpoint)
   .withHttpHeaders(defaultHeaders)
   .withRestRequestMethod(requestMethod)
   .build()

  ResponseObject respObj = WS.sendRequest(ro)
  return respObj
 }
}

As you can see, the source code includes two different ways to create a request. Both of them return a ResponseObject object. When using RestRequestObjectBuilder to create a RequestObject instance, you call the withXXX() and build() methods.

One benefit of RestRequestObjectBuilder is that it has additional methods to allow directly setting different body types for POST requests such as FileBodyContent and MultipartFormBodyContent. It is possible also using RequestObject, but it is more straightforward with RestRequestObjectBuilder.

For further details on the methods of these classes, please visit RequestObject and RestRequestObjectBuilder on the Katalon API Documentation website.

As a note, if you want to use HTTP headers, you can create new TestObjectProperty instances as shown in the code snippet.

Creating a POST request is basically the same as a GET request, but you must specify the request body using either setBodyContent from RequestObject or withTextBodyContent from RestRequestObjectBuilder.

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

public class SampleRequestObject {

 String endpoint = "https://www.katalon.com"
 String requestMethod = “POST "
 String authHeader = "whateverYouNeedForAuthentication"

 TestObjectProperty header1 = new TestObjectProperty("Authorization", ConditionType.EQUALS, authHeader)
 TestObjectProperty header2 = new TestObjectProperty("Content-Type", ConditionType.EQUALS, "application/json")
 TestObjectProperty header3 = new TestObjectProperty("Accept", ConditionType.EQUALS, "application/json")
 ArrayList defaultHeaders = Arrays.asList(header1, header2, header3)

 String body = '{"dummyRequest":"yes"}'

 /**
  * POST requests
  * @return
  */
 public ResponseObject buildPostApiRequest1() {
  RequestObject ro = new RequestObject("objectId")
  ro.setRestUrl(endpoint)
  ro.setHttpHeaderProperties(defaultHeaders)
  ro.setRestRequestMethod("POST")
  ro.setBodyContent(new HttpTextBodyContent(body))

  ResponseObject respObj = WS.sendRequest(ro)
  return respObj
 }

 public ResponseObject buildPostApiRequest2() {
  RequestObject ro = new RestRequestObjectBuilder()
   .withRestUrl(endpoint)
   .withHttpHeaders(defaultHeaders)
   .withRestRequestMethod("POST")
   .withTextBodyContent(body)
   .build()

  ResponseObject respObj = WS.sendRequest(ro)
  return respObj
 }
}

There are many different types of request body, such as file and form data body content. Please refer to the RestRequestObjectBuilder API documentation for a full list of methods.

ResponseObject

As you can see above, we get a ResponseObject instance when sending an API request. This object contains all information needed for a response, including status code, response body, response headers, waiting time and many others. Methods for handling response objects may look like this:

public class SampleResponseObject {

 public static int getStatusCode(ResponseObject resp) {
  return resp.getStatusCode()
 }
 public static String getResponseText(ResponseObject resp) {
  return resp.getResponseText()
 }
 public static long getResponseBodySize(ResponseObject resp) {
  return resp.getResponseBodySize()
 }
 public static long getResponseHeaderSize(ResponseObject resp) {
  return resp.getResponseHeaderSize()
 }
 public static long getWaitingTime(ResponseObject resp) {
  return resp.getWaitingTime()
 }
}

And sample use in your test case:

import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.util.KeywordUtil

import mypackage.SampleRequestObject
import mypackage.SampleResponseObject as SampleResponseObject

SampleRequestObject req = new SampleRequestObject()

ResponseObject resp = req.buildApiRequest1()

if (SampleResponseObject.getStatusCode(resp) != 200) {
 KeywordUtil.markFailed("Status code is not 200 as expected. It is " +
  SampleResponseObject.getStatusCode(resp))
}

if (SampleResponseObject.getWaitingTime(resp) > 5000) {
 KeywordUtil.markFailed("Your request takes more than 5000ms. Actual time is " +
  SampleResponseObject.getWaitingTime(resp))
}

You may wonder why the SampleResponseObject class is needed as you can get all the information directly by calling the methods from the response object. This wrapper may be helpful for your future enhancements as your code is less dependent on the ResponseObject class. If there are changes to this class, you only need to update the wrapper class. It is up to you which approach you follow, but both of them are correct.

Conclusion

This tutorial was focused on advanced usage of REST API requests in Katalon Studio. As I prefer writing code instead of clicking in manual mode, this tutorial may be helpful to fellow automation engineers. RequestObject and ResponseObject are powerful classes which may help you to customize your API tests in detail.

API REST Web Protocols Requests Katalon Studio

Opinions expressed by DZone contributors are their own.

Related

  • Building REST API Backend Easily With Ballerina Language
  • Composite Requests in Salesforce Are a Great Idea
  • CouchDB REST API for Document CRUD Operations — Examples With Postman
  • REST APIs: Simplicity, Flexibility, and Adoption

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!