Web Services Builder

Estimated reading time: 3 minutes

Since Katalon Studio 5.4

RestRequestObjectBuilder class provides functions to create a RESTful object easily and quickly in scripting view. You can find details regarding to API docs specifications from this page.

TypeName and description
RequestObjectbuild()
RestRequestObjectBuilderwithFileBodyContent(String filePath)
Specify the binary body content
RestRequestObjectBuilderwithHttpHeaders(List<TestObjectProperty> httpHeaderProperties)
Specify HTTP headers for the request object
RestRequestObjectBuilderwithMultipartFormDataBodyContent(List<FormDataBodyParameter> parameters)
Specify the body content for multipart/form-data type, default charset UTF-8
RestRequestObjectBuilderwithRestParameters(List<TestObjectProperty> restParameters)
Specify the REST parameters for the request object
RestRequestObjectBuilderwithRestRequestMethod(String requestMethod)
Specify method for the request object
RestRequestObjectBuilderwithRestUrl(String url)
RestRequestObjectBuilderwithTextBodyContent(String text)
Specify the text body content, default charset UTF-8
RestRequestObjectBuilderwithTextBodyContent(String text, String charset)
Specify the text body content
RestRequestObjectBuilderwithUrlEncodedBodyContent(List<UrlEncodedBodyParameter> parameters)
Specify the body content that composes of a list of url-encoded pairs, default charset UTF-8
RestRequestObjectBuilderwithUrlEncodedBodyContent(List<UrlEncodedBodyParameter> parameters, String charset)
Specify the body content that composes of a list of url-encoded pairs

Examples regard how to use them:

  1. Create a ‘GET’ RESTful 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.webservice.keyword.WSBuiltInKeywords as WS
        
    'Create a new GET object using builder'
    def builder = new RestRequestObjectBuilder()
    def requestObject = builder
    	.withRestRequestMethod("GET")
    	.withRestUrl("http://jsonplaceholder.typicode.com/comments")
    	.withRestParameters([
    		new TestObjectProperty("postId", ConditionType.EQUALS, "1"),
    		new TestObjectProperty("id", ConditionType.EQUALS, "1")
    	])
    	.withHttpHeaders([
    		new TestObjectProperty("Content-Type", ConditionType.EQUALS, "application/json")
    	])
    	.build()
    'Send a request'
    def response = WS.sendRequest(requestObject)
        
    'Verify if comment\'s email after sending request is correct or not'
    WS.verifyElementPropertyValue(response, '[0].email', 'Eliseo@gardner.biz')
        
        
    
  2. Create a ‘POST’ RESTful 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 builder = new RestRequestObjectBuilder()
          
    'Create a new POST object using builder'
    def requestObject = builder
        .withRestRequestMethod("POST")
        .withRestUrl("https://sample-web-service-aut.herokuapp.com/api/users/urlencoded")
        .withHttpHeaders([
            new TestObjectProperty("Content-Type", ConditionType.EQUALS, "application/x-www-form-urlencoded")
        ])
        .withUrlEncodedBodyContent([
            new UrlEncodedBodyParameter("username", "myUsername"),
            new UrlEncodedBodyParameter("password", "myPassword"),
            new UrlEncodedBodyParameter("gender", "MALE"),
            new UrlEncodedBodyParameter("age", "20"),
    		new UrlEncodedBodyParameter("avatar", "AAA"),
        ])
        .build()
             
    def response = WS.sendRequest(requestObject)
          
    assert response.getStatusCode() == 200