英文:
HP ALM file attachment
问题
我正在使用Java中的REST Assured对hp-alm进行API自动化,我正在尝试将附件上传到已创建的运行实例。
```java
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 received: " + statusCode);
状态码为500,错误是:
应提供文件名和内容。
这个错误是什么意思?有人可以告诉我代码有什么问题吗?
<div id="content-holder">
<h1>应提供文件名和内容。</h1>
<div>
<tr>
<td><a id="exception-id-title">异常ID:</a></td>
<td>qccore.general-error</td>
</tr>
</div>
</div>
除了使用REST Assured之外,是否有其他方法可以获得所需的输出?
<details>
<summary>英文:</summary>
I am doing an API automation of hp-alm using REST assured in Java. I'm trying to upload an attachment to a created run instance.
```java
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);
The status code is 500 and the error is:
> File name and content should be supplied.
What does the error mean? Can anyone tell whats wrong in my code?
<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>
Is there any method other then using rest assured to get desired output?
答案1
得分: 0
你的多部分请求不正确,你需要提供至少两部分:一个包含文件名的部分,以及一个包含文件本身的部分。
或者你可以使用 application/octet-stream
作为 Content-Type
。
在官方文档中有示例。
编辑:可行的代码示例:RestAssured.given().headers(urlheaders).cookies(loginCookies).cookies(sessionCookies).body(file);
而 urlheaders
必须包含:
- Content-Type = application/octet-stream
- Slug = 文件名
英文:
You have incorrect multipart request, you need to provide at least two parts: one which contains the file name, and one which contains the file itself.
Or you can use application/octet-stream Content-Type
instead.
See examples in the official docs.
Edit: working code: RestAssured.given().headers(urlheaders).cookies(loginCookies).cookies(sessionCookies).body(file);
And urlheaders must contain:
- Content-Type = application/octet-stream
- Slug = file name
答案2
得分: 0
根据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 received: " + statusCode);
这将把附件添加到运行实例中。
谢谢Sergi
<details>
<summary>英文:</summary>
As advised by Sergi,
The solution is:
```urlheaders.put("Content-Type", "application/octet-stream");
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
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论