Python 如何使用HP ALM REST API在QC中将图像上载为运行附件

Python 如何使用HP ALM REST API在QC中将图像上载为运行附件,python,rest,attachment,alm,hp-quality-center,Python,Rest,Attachment,Alm,Hp Quality Center,我找了好几天,试图自己解决这个问题,但没有成功 我发现可以通过以下方式(使用Python或Ruby)将文件附加到QC运行(在Rest请求中发送): 内容示例: headers = {'accept': 'application/xml', 'Content-Type': 'multipart/form-data; boundary=exampleboundary'} --exampleboundary Content-Disposition: form-data; name="filename

我找了好几天,试图自己解决这个问题,但没有成功

我发现可以通过以下方式(使用Python或Ruby)将文件附加到QC运行(在Rest请求中发送):

内容示例

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--
这确实有效,但(显然)仅适用于文本文件(.txt)。我真的需要上传一些图片,比如测试证据/截图

我怎样才能做到这一点?有人能帮我解决这个问题吗

我发送的请求内容如下:

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)
谢谢。

在API中:

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

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

关于巴尼的评论,我在这里留下了解决问题的答案

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
发件人:
感谢koxta分享此解决方案

有了这个解决方案,我可以成功地上传RobotFramework的日志文件作为测试运行的附件

共享我的代码:

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

您只需要根据要更改的文件类型更改内容类型upload@Barney对于图像(png和jpg),我可以使用哪种内容类型?文件的内容应该以字节为单位?你能举个例子吗?我将不胜感激。谢谢请参阅上传结果文件function@Barney是 啊就这样。工作得很好。非常感谢你。