简体   繁体   中英

HP ALM file attachment

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.

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?

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

As advised by Sergi, The solution is:

                    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM