1

I’ve searched for this issue and seems to be discussed a couple of times but with no real resolution.

I’m trying to upload an XML file using a POST request and form-data, but I get the following error response:

{
  "error":"The results file is required."
}

The error shows using ObjectRepository and also by code using with withMultipartFormDataBodyContent()

If I use curl it works fine. Also works fine with Postman.

Can someone please help me with this?

Thanks.

  • Welcome to SO! You have done your research, it is a good idea to include a couple of links in your question to help those that are comining to help you and for future reference. Maybe, try to post some code showing what you are doing, maybe it helps highlighting the issue – Daemon Painter Sep 16 '20 at 12:38
  • Hi! Thanks for the advice, you are right about it. Not an excuse but I was so tired of searching I didn't want to write anything more hehe. Next time I'll post some code but actually I've found the solution by myself. I'll answer my own post in case anyone else need it. Thanks again! – susacapuntas Sep 24 '20 at 14:22

1 Answers1

0

After a looooooooooooong time of searching and trying different things I already found the solution (that works for me). It uses Okhttp library so you will need to import it. If anyone else need it, there it is:

public void importJUnitTestExecRequest() {
    
    OkHttpClient client = new OkHttpClient();
    String reportFile = GlobalVariable.reportFolder + "\\JUnit_Report.xml";
    File file = new File(reportFile);

    String url = GlobalVariable.importTestExecJUnitEndpoint+"?testExecKey="+GlobalVariable.testExecKey;

    //Form request body that will be a multipart
    RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
            .addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("text/xml"), file))
            .build();

    //Form the actual request adding necessary headers
    Request request = new Request.Builder()
            .url(url)
            .post(requestBody)
            .addHeader("Content-Type", GlobalVariable.contentTypeMultipart)
            .build();

    Response response = null;

    try {
        response = client.newCall(request).execute();
        println("************ IMPORT TEST EXECUTION RESULTS RAW RESPONSE ************");
        println("Response status: " + response);
        println("********************************************************************");
        if (response.isSuccessful()){
            String responseBody = response.body().string();
            println("************ IMPORT TEST EXECUTION RESULTS RESPONSE BODY ************");
            println(responseBody);
            println("*********************************************************************");
        } else {
            throw new IOException("Unexpected HTTP code " + response);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

}

I've opened a ticket to support because with the built in functionality in Katalon, it is currently (or I don't know how to do it) not possible.