【问题标题】:HP ALM 11 Upload attachment using PHP and cURLHP ALM 11 使用 PHP 和 cURL 上传附件
【发布时间】:2013-03-24 19:30:34
【问题描述】:

我需要将附件上传到 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.&#xD;
at org.hp.qc.web.restapi.attachments.AttachmentsResource.createAttachmentMutliPart(AttachmentsResource.java:141)&#xD;
at sun.reflect.GeneratedMethodAccessor985.invoke(Unknown Source)&#xD;
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)&#xD;
at java.lang.reflect.Method.invoke(Method.java:597)&#xD;
...

我想知道我做错了什么。我正在遵循 API 参考中 POST 字段的指示结构。 谢谢。

【问题讨论】:

    标签: php rest curl hp-quality-center


    【解决方案1】:

    我刚刚解决了这个问题,我很高兴。我的问题是我在标题中用破折号定义边界,就像在正文中引用它们一样。这是不正确的。

    这是一个对我有用的示例(基于 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.txt”的简单文本文件,其内容为“Hello!!”它是使用上面的代码生成的:

    标题:

    '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--
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-06
      相关资源
      最近更新 更多