使用PHP和cURL的HP ALM 11 Upload附件 [英] HP ALM 11 Upload attachment using PHP and cURL

查看:82
本文介绍了使用PHP和cURL的HP ALM 11 Upload附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将附件上传到HP ALM 11中的测试. 为此,我创建了一个基于cURL的自定义PHP函数,以使用ALM的REST API. 这是代码:

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);
}

当我将请求发送到指定的URL时,我从ALM收到跟随错误:

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.&#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字段的指示结构. 谢谢.

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.

以下是为我工作的示例(基于REST API文档):

Here is a sample (based on the REST API docs) that worked for me:

标题:

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

我仍然无法上传二进制附件,但这对我来说是一个很大的进步.

I am still not able to upload binary attachments, but this is a big improvement for me.

编辑-以下powershell代码对我有用,可用于上传任何文件类型:

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

这是我的最新更新-python函数,可通过REST API成功上传附件. 请注意,它是一个更大的库的一部分,但希望您能想到:

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.

由于代码是较大库的一部分,因此该代码无法正常工作.这是一个示例,该示例介绍了如何发布一个名为"hello.txt"的简单文本文件,其内容为"Hello !!".它是使用上面的代码制作的:

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:

标题:

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

这篇关于使用PHP和cURL的HP ALM 11 Upload附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆