问题描述
我正在使用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