HP ALM 11 Upload attachment using PHP and cURL

旧时模样 提交于 2019-12-09 05:48:26

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--
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!