How to Use setBodyContent(HttpBodyContent) in Katalon

How to Use setBodyContent(HttpBodyContent) in Katalon

26 February 2025 Stephan Petzl Leave a comment Katalon Issues

Updating test management APIs with the latest test results is a common requirement in many testing environments, especially when integrating with tools like JIRA. In this guide, we will walk you through the process of using the setBodyContent(HttpBodyContent) method in Katalon to achieve this task effectively.

Understanding the Requirement

In scenarios where you need to update an API with a POST request, such as adding test results to Adaptavist Test Management in JIRA, it’s crucial to send the correct body content with your request. The challenge often arises when using Katalon’s scripting capabilities, especially with deprecated methods like setHttpBody. Let’s explore how to transition to the setBodyContent(HttpBodyContent) method.

Implementing setBodyContent

The setBodyContent method allows you to specify the content of the HTTP body when making a request. Depending on the type of content you’re working with (text, file, form data, or URL encoded), you’ll need to choose the appropriate implementation. For text body content, which is common in API updates, the following approach is recommended:

Step-by-Step Guide

  1. Import the necessary classes for body content:
  2. import com.kms.katalon.core.testobject.impl.HttpTextBodyContent
    import com.kms.katalon.core.testobject.impl.HttpFileBodyContent
    import com.kms.katalon.core.testobject.impl.HttpFormDataBodyContent
    import com.kms.katalon.core.testobject.impl.HttpUrlEncodedBodyContent
            
  3. Use the HttpTextBodyContent for text-based body content:
  4. setBodyContent(new HttpTextBodyContent(your_text))
            

Practical Example

Suppose you want to update a JIRA issue with the results of a Katalon test. You would construct the request body with the relevant details and set it using setBodyContent:

// Construct the request object
RequestObject getJIRAUpdateObject = (RequestObject)findTestObject('Web Service Calls/Update JIRA')

// Define the request body
String vsRequestBody = '{\"projectKey\": \"FVS\", \"testCaseKey\": \"FVS-T1\", \"status\": \"Pass\", \"environment\": \"DEV\"}';

// Set the body content
getJIRAUpdateObject.setBodyContent(new HttpTextBodyContent(vsRequestBody))

// Send the request
WS.sendRequest(getJIRAUpdateObject)
    

Enhancing Your Testing Workflow with Repeato

While Katalon provides robust capabilities for integrating with external APIs, tools like Repeato offer additional advantages. Repeato is a No-code test automation tool that simplifies creating, running, and maintaining automated tests for iOS, Android, and web apps. Unlike Katalon, Repeato supports a broader range of scripting languages and offers open-source flexibility, making it easier to adapt to specific project needs.

With features like data-driven testing, command line script execution, and seamless integration with version control systems, Repeato can streamline your testing processes, reduce setup time, and enhance test accuracy. For more detailed insights, you can explore our documentation.

By leveraging Repeato alongside Katalon, you can achieve a more comprehensive and agile testing framework, ensuring your applications meet the highest quality standards.

Like this article? there’s more where that came from!