About Rest API Patch call

I am converting test executions from Powershell to Katalon.
But, katalon does not support Rest API PATCH call.

Is there a way to execute powershell call, like this one:
$global:response = Invoke-WebRequest -Method Patch -Uri $SiteURL -Headers $Header -ContentType “application/json; charset=utf-8” -Body $Body

And then proceed with values from $global:response, which is in json format?

1 Like

Hello,
if nobody will offer more “Katalon” solution, there is allways way to do it in groovy.:

We have found this solution:
https://communities.ca.com/thread/241759991-rest-call-with-method-patch

Works quite well for us.

Dejan Dzodan said:

We have found this solution:
https://communities.ca.com/thread/241759991-rest-call-with-method-patch

Works quite well for us.

Hi Dejan, could you please provide some more detail on how you implemented this in Katalon? Did you place the code inside of a custom keyword?

Heres my solution. I’ve implemented it as a Keyword which you pass the URL, Identifier (In my case a guid) and the json string you are attempting to PATCH. It then returns the API response as an object which can be tested against. Still really confused as to why PATCHing is not supported in Katalon:

package customMethods
import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.checkpoint.Checkpoint
import com.kms.katalon.core.checkpoint.CheckpointFactory
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testcase.TestCase
import com.kms.katalon.core.testcase.TestCaseFactory
import com.kms.katalon.core.testdata.TestData
import com.kms.katalon.core.testdata.TestDataFactory
import com.kms.katalon.core.testobject.ObjectRepository
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords
import internal.GlobalVariable
import MobileBuiltInKeywords as Mobile
import WSBuiltInKeywords as WS
import WebUiBuiltInKeywords as WebUI
import okhttp3.MediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.Response
import groovy.json.JsonSlurper
public class HTTPRequest {
@Keyword
def PATCH(String URL, String Identifier, String JSONBody) {

OkHttpClient client = new OkHttpClient()
MediaType mediaType = MediaType.parse("application/json")
String url = URL + '/' + Identifier
String requestBody = JSONBody
RequestBody body = RequestBody.create(mediaType, requestBody)
Request request = new Request.Builder()
.url(url)
.patch(body)
.addHeader("content-type", "application/json")
.addHeader("cache-control", "no-cache")
.build();
Response response = client.newCall(request).execute()
String responseBodyString = response.body().string()
//Create a new jsonSlurper object to manipulate the response from the API
JsonSlurper jsonSlurper = new JsonSlurper()
//Create a new object which will represent the response data from the API
def responseObject = jsonSlurper.parseText(responseBodyString)
return responseObject
}
}

Justin Harper said:

Heres my solution. I’ve implemented it as a Keyword which you pass the URL, Identifier (In my case a guid) and the json string you are attempting to PATCH. It then returns the API response as an object which can be tested against. Still really confused as to why PATCHing is not supported in Katalon:

package customMethods

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.checkpoint.Checkpoint
import com.kms.katalon.core.checkpoint.CheckpointFactory
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testcase.TestCase
import com.kms.katalon.core.testcase.TestCaseFactory
import com.kms.katalon.core.testdata.TestData
import com.kms.katalon.core.testdata.TestDataFactory
import com.kms.katalon.core.testobject.ObjectRepository
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords
import internal.GlobalVariable
import MobileBuiltInKeywords as Mobile
import WSBuiltInKeywords as WS
import WebUiBuiltInKeywords as WebUI
import okhttp3.MediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.Response
import groovy.json.JsonSlurper
public class HTTPRequest {
@Keyword
def PATCH(String URL, String Identifier, String JSONBody) {

OkHttpClient client = new OkHttpClient()
MediaType mediaType = MediaType.parse(“application/json”)
String url = URL + ‘/’ + Identifier
String requestBody = JSONBody
RequestBody body = RequestBody.create(mediaType, requestBody)
Request request = new Request.Builder()
.url(url)
.patch(body)
.addHeader(“content-type”, “application/json”)
.addHeader(“cache-control”, “no-cache”)
.build();
Response response = client.newCall(request).execute()
String responseBodyString = response.body().string()
//Create a new jsonSlurper object to manipulate the response from the API
JsonSlurper jsonSlurper = new JsonSlurper()
//Create a new object which will represent the response data from the API
def responseObject = jsonSlurper.parseText(responseBodyString)
return responseObject
}
}


  
  
  

  

I try to use your script in katalon 5.7.1 and I can import

import okhttp3.MediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.Response

any idea what is happened , How can import thats libraries

Guillermo Torres said:

Justin Harper said:

Heres my solution. I’ve implemented it as a Keyword which you pass the URL, Identifier (In my case a guid) and the json string you are attempting to PATCH. It then returns the API response as an object which can be tested against. Still really confused as to why PATCHing is not supported in Katalon:

package customMethods

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.checkpoint.Checkpoint
import com.kms.katalon.core.checkpoint.CheckpointFactory
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testcase.TestCase
import com.kms.katalon.core.testcase.TestCaseFactory
import com.kms.katalon.core.testdata.TestData
import com.kms.katalon.core.testdata.TestDataFactory
import com.kms.katalon.core.testobject.ObjectRepository
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords
import internal.GlobalVariable
import MobileBuiltInKeywords as Mobile
import WSBuiltInKeywords as WS
import WebUiBuiltInKeywords as WebUI
import okhttp3.MediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.Response
import groovy.json.JsonSlurper
public class HTTPRequest {
@Keyword
def PATCH(String URL, String Identifier, String JSONBody) {

OkHttpClient client = new OkHttpClient()
MediaType mediaType = MediaType.parse(“application/json”)
String url = URL + ‘/’ + Identifier
String requestBody = JSONBody
RequestBody body = RequestBody.create(mediaType, requestBody)
Request request = new Request.Builder()
.url(url)
.patch(body)
.addHeader(“content-type”, “application/json”)
.addHeader(“cache-control”, “no-cache”)
.build();
Response response = client.newCall(request).execute()
String responseBodyString = response.body().string()
//Create a new jsonSlurper object to manipulate the response from the API
JsonSlurper jsonSlurper = new JsonSlurper()
//Create a new object which will represent the response data from the API
def responseObject = jsonSlurper.parseText(responseBodyString)
return responseObject
}
}


  
  
  

  

I try to use your script in katalon 5.7.1 and I can import

import okhttp3.MediaType

import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.Response


any idea what is happened , How can import thats libraries

  

I followed this guide: https://communities.ca.com/thread/241759991-rest-call-with-method-patch

Make sure you get the same versions stated in the guide otherwise it wont work. If you then import them into your project by going to Project / Settings / External Libraries, you should be good to go!

Screen Shot 2018-09-24 at 09.32.49.png

1 Like

Hopefully we wont need to do this for to long as PATCH is coming to Katalon!: http://forum.katalon.com/discussion/comment/22811#Comment_22811

1 Like