【问题标题】:How to upload IMAGE as run attachment in QC with HP ALM REST API如何使用 HP ALM REST API 在 QC 中将 IMAGE 作为运行附件上传
【发布时间】:2017-07-12 17:28:27
【问题描述】:

我已经搜索了几天并试图自己解决问题,但没有成功。

我发现可以使用类似这样的方式(在 Rest Request 中发送)将文件附加到 QC Run(使用 Python 或 Ruby):

内容示例

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)

谢谢。

【问题讨论】:

  • 您只需要根据您要上传的文件类型更改Content-Type
  • @Barney 对于图像(png 和 jpg),我可以使用哪种内容类型?文件的内容应该以字节为单位?你能举个例子吗?我会很感激的。谢谢。
  • @Barney 是的。而已。工作完美。非常感谢。

标签: python rest attachment alm hp-quality-center


【解决方案1】:

参考 Barney 的评论,我将解决问题的答案留在这里。

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

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

【讨论】:

    【解决方案2】:

    在 API 中:

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

    在 POST 调用中:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-24
        相关资源
        最近更新 更多