HP ALM文件附件

huangapple go评论77阅读模式
英文:

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&#39;m trying to upload an attachment to a created run instance.

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

&lt;div id=&quot;content-holder&quot;&gt;
	&lt;h1&gt;File name and content should be supplied&lt;/h1&gt;
	&lt;div&gt;
		&lt;tr&gt;
			&lt;td&gt;&lt;a id=&quot;exception-id-title&quot;&gt;Exception Id:&lt;/a&gt;&lt;/td&gt;
			&lt;td&gt;qccore.general-error&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/div&gt;
&lt;/div&gt;

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(&quot;Content-Type&quot;, &quot;application/octet-stream&quot;);
					File file = new File(filePath);
					urlheaders.put(&quot;slug&quot;, file.getName());
					RequestSpecification httpRequest14 = RestAssured.given().headers(urlheaders).cookies(loginCookies).cookies(sessionCookies).body(file);
					Response attachment = httpRequest14.request(Method.POST,&quot;/qcbin/rest/domains/&quot;+domain+&quot;/projects/&quot;+project+&quot;/runs/&quot;+stepID+&quot;/attachments&quot;);
					String attachmentResponseBody = attachment.getBody().asString();
					logger.info(&quot;attachment Response Body is =&gt;  &quot; + attachmentResponseBody);
					statusCode = attachment.getStatusCode();
					logger.info(&quot;The attachment status code recieved: &quot; + statusCode);

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

</details>



huangapple
  • 本文由 发表于 2020年10月19日 21:31:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64428438.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定