koxta
koxta

Reputation: 916

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.

Upvotes: 1

Views: 3118

Answers (3)

CHIEN-CHUN WANG
CHIEN-CHUN WANG

Reputation: 1

Thank koxta for sharing this solution.

With this solution, I can upload a RobotFramework's log file as an attachment of a test run successfully.

Share my code:

def upload_log(self, entity_type, entity_id, file_name):
    qurl = '%s/qcbin/rest/domains/%s/projects/%s/%s/%s/attachments' %(self._url, self._domain, self._project, entity_type, entity_id)

    headers = self._headers
    headers['Content-Type'] = 'application/octet-stream'
    headers['slug'] = 'log.' +file_name[file_name.rfind(".")+1: ]
    print (headers)

    if os.path.isfile(file_name):
        with open(file_name, 'rb') as log_file:
            binary_data = log_file.read()
    print (binary_data)

    response = self.session.post(qurl, data=binary_data, headers=headers)
    print (response.text)
    if response.status_code != 201:
        raise Exception('Failed to upload %s - code=%s message=%s' %(file_name, response.status_code, response.text))

Upvotes: 0

koxta
koxta

Reputation: 916

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

Upvotes: 2

Roy Holzem
Roy Holzem

Reputation: 870

In API:

if not request.form:
    abort(405)
request.form.get('file', "")
file = file.read().encode("base64")

In POST call:

 -F 'file=@/var/path/to/my/file/test.png' http://xxx.xx.xx.xxx:8080/todo/api/v1.0/tasks

Upvotes: 0

Related Questions