Search code examples
pythonrestattachmentalmhp-quality-center

How to upload IMAGE as run attachment in QC with HP ALM REST API


I have searched for days and trying to solve the problem by myself, unsuccessfully.

I found that is possible to attach files to QC Run (using Python or Ruby) with something like this (send it in a Rest Request):

Content example:

headers = {'accept': 'application/xml', 'Content-Type': 'multipart/form-data; boundary=exampleboundary'}

--exampleboundary
Content-Disposition: form-data; name="filename"
example.txt

--exampleboundary
Content-Disposition: form-data; name="description"
Here is the text that describes example.txt

--exampleboundary
Content-Disposition: form-data; name="file"; filename="example.txt"
Content-Type: text/plain
ContentOfFile

--exampleboundary--

This really works but (apparently) only for text files (.txt). I really need to upload some images like test evidences/screenshots.

How can I achieve that? Can anyone help me to solve this problem?

I am sending the request content like this:

import requests
#login
response = requests.get("http://"+server+"/qcbin/authentication-point/authenticate", auth=(user,pwd))

# Get ALM token in dict format
token = response.cookies.get_dict()

requests.post(url, content, cookies=token, headers=headers_image)

Thank you.


Solution

  • Referring to Barney's comment I leave here the answer that solved the problem.

    def upload_result_file(self, run_id, report_file, token):
    
        url = "http://%s/qcbin/rest/domains/%s/projects/%s/runs/%s/attachments" % (server, domain, project, run_id)
    
        payload = open(report_file, 'rb')
        headers_file = {}
        headers_file['Content-Type'] = "application/octet-stream"
        headers_file['slug'] = "test-results." + report_file[report_file.rfind(".")+1: ]
    
        response = requests.post(url, headers=headers_file, data=payload, cookies=token)
        if not (response.status_code == 200 or response.status_code == 201):
            print "Attachment step failed!", response.text, response.url, response.status_code
        return
    

    From: https://github.com/macroking/ALM-Integration/blob/master/ALM_Integration_Util.py