HP ALM 文件附件

HP ALM file attachment

我正在使用 Java 中的 REST 保证对 hp-alm 进行 API 自动化。我正在尝试将附件上传到已创建的 运行 实例。

urlheaders.put("Content-Type", "multipart/form-data");
File file = new File("H:\Desktop\a.zip");
RequestSpecification httpRequest14 = RestAssured.given().headers(urlheaders).cookies(loginCookies).cookies(sessionCookies).multiPart( "file", file, "multipart/form-data");
Response attachment = httpRequest14.request(Method.POST,"/qcbin/rest/domains/d/projects/p/runs/13634/attachments");
String attachmentResponseBody = attachment.getBody().asString();
//logger.info("attachment Response Body is =>  " + attachmentResponseBody);
statusCode = attachment.getStatusCode();
logger.info("The attachment status code recieved: " + statusCode);

状态代码为 500,错误为:

File name and content should be supplied.

错误是什么意思?谁能告诉我我的代码有什么问题?

<div id="content-holder">
    <h1>File name and content should be supplied</h1>
    <div>
        <tr>
            <td><a id="exception-id-title">Exception Id:</a></td>
            <td>qccore.general-error</td>
        </tr>
    </div>
</div>

除了放心使用之外,还有什么方法可以得到想要的输出吗?

您的多部分请求不正确,您需要至少提供两部分:一部分包含文件名,另一部分包含文件本身。

或者您可以改用 application/octet-stream Content-Type。 请参阅 official docs.

中的示例

编辑:工作代码:RestAssured.given().headers(urlheaders).cookies(loginCookies).cookies(sessionCookies).body(file); 并且 urlheaders 必须包含:

  • Content-Type = application/octet-stream
  • Slug = 文件名

根据 Sergi 的建议, 解决方案是:

                    File file = new File(filePath);
                    urlheaders.put("slug", file.getName());
                    RequestSpecification httpRequest14 = RestAssured.given().headers(urlheaders).cookies(loginCookies).cookies(sessionCookies).body(file);
                    Response attachment = httpRequest14.request(Method.POST,"/qcbin/rest/domains/"+domain+"/projects/"+project+"/runs/"+stepID+"/attachments");
                    String attachmentResponseBody = attachment.getBody().asString();
                    logger.info("attachment Response Body is =>  " + attachmentResponseBody);
                    statusCode = attachment.getStatusCode();
                    logger.info("The attachment status code recieved: " + statusCode);

This will add the attachment to the run instance.
Thank You Sergi