微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

HP ALM文件附件

如何解决HP ALM文件附件

我正在使用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,错误为:

应提供文件名和内容

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

<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); 网址标题必须包含:

  • Content-Type =应用程序/八位字节流
  • Slug =文件名
,

根据塞尔吉的建议, 解决方案是:

                    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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。