I need to upload an attachment to a test in HP's ALM 11. To do so, I have created a custom PHP function based in cURL to use the REST API of ALM. This is the code:
public function attachment($project, $domain, $entity, $id, $filename)
{
$qc = $this->qc_cookie;
$ckfile = $this->ckfile;
$eol = "\n\n";
$mime_boundary=md5(time());
$file_to_upload = '--'.$mime_boundary. $eol;
$file_to_upload .= 'Content-Disposition: form-data; name="filename"';
$file_to_upload .= $eol;
$file_to_upload .= $filename;
$file_to_upload .= $eol;
$file_to_upload .= $eol;
$file_to_upload .= '--'.$mime_boundary. $eol;
$file_to_upload .= 'Content-Disposition: form-data; name="description"';
$file_to_upload .= $eol;
$file_to_upload .= 'Test';
$file_to_upload .= $eol;
$file_to_upload .= $eol;
$file_to_upload .= '--'.$mime_boundary. $eol;
$file_to_upload .= 'Content-Disposition: form-data; name="file"; filename="'.$filename.'"';
$file_to_upload .= $eol;
$file_to_upload .= 'Content-Type: text/plain';
$file_to_upload .= $eol;
$file_to_upload .= 'Content-Transfer-Encoding: base64' . $eol . $eol;
$handle = fopen($filename, 'r');
$file_to_upload .= fread($handle,filesize($filename));
$file_to_upload .= $eol;
$file_to_upload .= '--'.$mime_boundary.'--'. $eol.$eol;
fclose($handle);
$header = array("POST /qcbin/rest/domains/".$domain."/projects/".$project."/".$entity.'/'.$id.'/attachments'." HTTP/1.1",
'Content-Type: multipart/form-data; boundary='.$mime_boundary,
//'Content-Length: '.strlen($file_to_upload)
);
curl_setopt($qc, CURLOPT_HTTPHEADER, $header);
curl_setopt($qc, CURLOPT_POSTFIELDS, $file_to_upload);
curl_setopt($qc, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($qc, CURLOPT_RETURNTRANSFER, true);
curl_setopt($qc, CURLOPT_URL, $this->url."/qcbin/rest/domains/".$this->domain."/projects/".$this->project."/".$entity."/".$id.'/attachments');
return curl_exec($qc);
}
When I send the request to the specified URL, I get the followin error from ALM:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<QCRestException>
<Id>qccore.general-error</Id>
<Title>Illegal multi-part arguments. Attachment wasn't created.</Title>
<StackTrace>java.lang.IllegalArgumentException: Illegal multi-part arguments. Attachment wasn't created.
at org.hp.qc.web.restapi.attachments.AttachmentsResource.createAttachmentMutliPart(AttachmentsResource.java:141)
at sun.reflect.GeneratedMethodAccessor985.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
...
I'd like to know what I'm doing wrong. I'm following the indicated structure of the POST fields that is in the API reference. Thank you.
I just solved this problem and am very happy. My issue was that I was defining the boundary in the header with dashes just as they're referenced in the body. This was not correct.
Here is a sample (based on the REST API docs) that worked for me:
Headers:
Content-Type: multipart/form-data; boundary=myboundary
Body:
--myboundary
Content-Disposition: form-data; name="filename"
anna.txt
--myboundary
Content-Disposition: form-data; name="file"; filename="anna.txt"
banana
--myboundary--
I am still not able to upload binary attachments, but this is a big improvement for me.
Edit - the following powershell code worked for me for uploading any file type:
$contents = gc "my file path"
$body = @"
--boundary
Content-Disposition: form-data; name="filename"
$name
--boundary
Content-Disposition: form-data; name="file"; filename="$name"
$contents
--boundary--
"@
$headers = @{"Content-Length" = $body.Length; "Content-Type" = "multipart/form-data; boundary=$boundary"}
Invoke-RestMethod -Uri "ALM attachment URI" -Method Post -ContentType "multipart/form-data;boundary=boundary" -WebSession $global:session -Body $body
Edit: Here's my last update - a python function that successfully uploads an attachment via REST API. Note that it's part of a larger library, but hopefully you get the idea:
def upload_attachment(self, entity_type, id, file_name):
"""
Name
upload_attachment
Description
Uploads an attachment to the supplied entity
Parameters
entity_type: the entity type - i.e. 'test-instance', 'run', etc.
id: the integer id of the entity
file_name: the name of the file in the local filesystem
Returns
"""
print 'Uploading attachment to %s id=%s' % (entity_type, id)
with open(file_name, 'rb') as file:
bin_data = file.read()
qurl = '%s/qcbin/rest/domains/%s/projects/%s/%s/%s/attachments' % \
(self.url, self.domain, self.project, entity_type, id)
headers = {'Content-Type' : 'multipart/form-data; boundary=uploadboundary'}
body = """--uploadboundary
Content-Disposition: form-data; name="filename"
%s
--uploadboundary
Content-Disposition: form-data; name="file"; filename="%s"
%s
--uploadboundary--""" % (file_name, file_name, bin_data)
rsp = self.session.post(qurl, data=body, headers=headers)
if rsp.status_code != 201:
raise Exception('Failed to upload %s - code=%s message=%s' % \
(file_name, rsp.status_code, rsp.text))
I hope someone finds this useful.
Edit: The code was not working since it's part of a larger library. Here is an example of how to post a simple text file named 'hello.txt' with the content 'Hello!!' It was produced with the code from above:
Headers:
'Content-Type': 'multipart/form-data; boundary=uploadboundary'
Body:
--uploadboundary
Content-Disposition: form-data; name="filename"
hello.txt
--uploadboundary
Content-Disposition: form-data; name="file"; filename="hello.txt"
Hello!!
--uploadboundary--
来源:https://stackoverflow.com/questions/15782162/hp-alm-11-upload-attachment-using-php-and-curl