问题描述
我已经搜索了这个问题,似乎已经讨论了几次,但没有真正的解决方法。
我正在尝试使用POST请求和表单数据上传XML文件,但收到以下错误响应:
{
"error":"The results file is required."
}
错误显示使用ObjectRepository以及通过代码使用withMultipartFormDataBodyContent()
如果我使用卷曲,效果很好。也可以与Postman一起使用。
有人可以帮我吗?
谢谢。
解决方法
经过漫长的搜索和尝试其他操作之后,我已经找到了解决方案(对我有用)。它使用Okhttp库,因此您需要导入它。 如果其他人需要它,它是:
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();
}
}
我已经开出了一张罚单以进行支持,因为借助Katalon中的内置功能,目前(或者我不知道如何做到)是不可能的。