如何使用 JAVA 中的 Multipart POST 调用将图像发送到 Testrail 中的 Testrun?

问题描述 投票:0回答:1

我正在尝试附上测试失败后拍摄的屏幕截图。屏幕截图被捕获并存储在文件夹中。我正在尝试使用 Multipart Post 调用将该文件夹中的屏幕截图发送到 Testrail,并且收到 400 响应。

我尝试通过邮寄电话发送图像的代码,我通过此邮寄电话收到 400 响应。

有谁知道获得 400 响应背后的原因,是否有任何需要在代码级别进行修改的内容?是否有其他替代方法可以通过调用后将图像发送到 Testrail 中的测试运行?

HttpClient httpclient = new DefaultHttpClient();

httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

String rawURL = ".../index.php?/api/v2/add_attachment_to_result/{result_id}";
URL url = new URL(rawURL);
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.setRequestProperty("Authorization",
  "Basic " + Base64.getEncoder().encodeToString(
            (userName + ":" + userPassword).getBytes()
          )
        );

HttpPost httppost = new HttpPost(".../index.php?/api/v2/add_attachment_to_result/{result_id}");
File file = new File("file path");

MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file, "image/jpeg");
mpEntity.addPart("userfile", cbFile);


httppost.setEntity(mpEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();

System.out.println(response.getStatusLine());
if (resEntity != null) {
  System.out.println(EntityUtils.toString(resEntity));
}
if (resEntity != null) {
  resEntity.consumeContent();
}

httpclient.getConnectionManager().shutdown();

我通过此帖子收到了 400 回复。

selenium-webdriver automation http-post testrail
1个回答
0
投票

尝试使用多部分实体的构建器:

            final MultipartEntityBuilder multipartBuilder = MultipartEntityBuilder.create().addBinaryBody("attachment", file);
            post.setEntity(multipartBuilder.build());
© www.soinside.com 2019 - 2024. All rights reserved.