[英]HP ALM 11 Upload attachment using PHP and cURL
我需要在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字段結構。 謝謝。
我剛剛解決了這個問題而且非常高興。 我的問題是我在標題中用破折號定義邊界,正如它們在正文中被引用一樣。 這不對。
以下是一個適合我的示例(基於REST API文檔):
頭:
Content-Type: multipart/form-data; boundary=myboundary
身體:
--myboundary
Content-Disposition: form-data; name="filename"
anna.txt
--myboundary
Content-Disposition: form-data; name="file"; filename="anna.txt"
banana
--myboundary--
我仍然無法上傳二進制附件,但這對我來說是一個很大的改進。
編輯 - 以下powershell代碼適用於我上傳任何文件類型:
$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
編輯:這是我的上一次更新 - 一個通過REST API成功上傳附件的python函數。 請注意,它是更大的庫的一部分,但希望您明白這一點:
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))
我希望有人覺得這很有用。
編輯:代碼無法正常工作,因為它是更大的庫的一部分。 這是一個如何使用內容'Hello !!'發布一個名為'hello.txt'的簡單文本文件的示例 它是用上面的代碼生成的:
頭:
'Content-Type': 'multipart/form-data; boundary=uploadboundary'
身體:
--uploadboundary
Content-Disposition: form-data; name="filename"
hello.txt
--uploadboundary
Content-Disposition: form-data; name="file"; filename="hello.txt"
Hello!!
--uploadboundary--
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.