我需要将附件上传到 HP 的 ALM 11 中的测试。为此,我创建了一个基于 cURL 的自定义 PHP 函数以使用 ALM 的 REST API。这是代码:
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);
}
当我将请求发送到指定的 URL 时,我从 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)
...
我想知道我做错了什么。我正在遵循 API 参考中 POST 字段的指示结构。谢谢你。