Groovy code for a Rest API request

Hello, I have this curl request:

curl -H Content-Type:text/xml -X POST -H "Authorization: Bearer MY_TOKEN" --data @/path/to/JUnit_Report.xml https://host/api/v1/import/execution/junit?projectKey=XXX

It will at the end export tests results to Jira. In bbash I don’t have any issue with this request.

I tried to do send it with Katalon in script mode, but didn’t succeed…
This is what I have done: (several methods)

First method:

ProcessBuilder pb2 = new ProcessBuilder(
"curl", "-H", "Content-Type:text/xml", "-H", "\"Authorization: Bearer " + MY_TOKEN + "\"", "-X", "POST", "--data", 
"@/path/to/JUnit_Report.xml", 
"https://host/api/v1/import/execution/junit?projectKey=XXX"
);
pb2.redirectErrorStream(true);
Process p2 = pb2.start();

Curl request seems to not fail, but no data are exported to Jira.

Second method:

TestObjectProperty auth = new TestObjectProperty()
auth.setName("Authorization")
auth.setValue("Bearer " + token)
TestObjectProperty content = new TestObjectProperty()
content.setName("Content-Type")
content.setValue("text/xml")
 
def headers = new ArrayList()
headers.add(auth)
headers.add(content)
TestObjectProperty param = new TestObjectProperty()
param.setName("--data")
param.setValue("@/path/to/JUnit_Report.xml")
def params = new ArrayList()
params.add(param)
 
RequestObject reqObj = new RequestObject()
reqObj.setHttpHeaderProperties(headers)
reqObj.setRestRequestMethod('POST')
reqObj.setRestUrl('https://host/api/v1/import/execution/junit?projectKey=XXX')
reqObj.setRestParameters(params)
def getResponse = WSBuiltInKeywords.sendRequest(reqObj)

This didn’t work (NPE) but I don’t even know if it is correctly implemented. Documentation does not help.

Is there someone who has an idea?

Thanks

Hi Quentin,

at which line you get NullPointerException? At least second method looks correct.

Hello Marek,
Support team from the plugin I used sent me a solution with okhttp3 library. I will share it in case it can help other people also:

MediaType MEDIA_TYPE_XML = MediaType.parse("application/xml")
RequestBody rb = null;
try {
String junit_report_filename = RunConfiguration.getProjectDir() + "/path/to/JUnit_Report.xml";
rb = RequestBody.create(MEDIA_TYPE_XML, new File(junit_report_filename));
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String token = "$tokenResult";
OkHttpClient client = new OkHttpClient();
String endpoint_url = "https://host/api/v1/import/execution/junit";
HttpUrl url = HttpUrl.parse(endpoint_url).newBuilder().setQueryParameter("projectKey", "XXX").build();
Request request = new Request.Builder().url(url).post(rb).addHeader("Authorization", "Bearer " + token).build();
Response response = null;
try {
response = client.newCall(request).execute();
System.out.println("=============");
System.out.println(response);
String response_body = response.body().string();
System.out.println(response_body);
System.out.println("=============");
if (response.isSuccessful()){
JSONObject responseObj = new JSONObject(response_body);
System.out.println("Test Execution: "+(responseObj.get("key")));
} else {
throw new IOException("Unexpected HTTP code " + response);
}
} catch (IOException e) {
e.printStackTrace();
}