求助:如何通过REST API以URL方式向ALM上传附件
实现通过ALM REST API从外部URL上传附件
我已经掌握了通过REST API以二进制形式向ALM上传本地附件的方法,Python代码如下:
URL = "https://some.almserver.com/qcbin/rest/domains/DOMAIN/projects/PROJECT/runs/<run-id>/attachments" METHOD = "POST" HEADERS = { 'cache-control': "no-cache", "Slug": "some.html", 'Content-Type': "application/octet-stream" } def attach_to_run_step(): f = open("/path/to/some.html", "rb") response = requests.post(URL, headers=HEADERS, cookies=cookies, data=f.read()) print(response.content)
但目前无法找到通过REST API直接将外部URL中的附件上传至ALM的方法(ALM UI界面支持该功能),需要获取实现该需求的请求方法、请求头及请求负载信息。
解决方案:通过元数据请求创建外部链接附件
请求方法与端点
使用POST请求,端点和本地文件上传一致:https://some.almserver.com/qcbin/rest/domains/DOMAIN/projects/PROJECT/runs/<run-id>/attachments
请求头
需要设置以下请求头:
cache-control: no-cacheSlug: <附件显示名称>(比如external-report.pdf)Content-Type: application/xml(ALM多数版本优先支持XML格式,部分新版本也支持application/json)Accept: application/xml(对应响应格式)- 保持登录后的
cookies(和本地上传一致)
请求负载(XML格式)
请求体需要提交包含外部URL信息的XML元数据,示例如下:
<Attachment> <FileName>external-report.pdf</FileName> <Link>https://external-server.com/files/report.pdf</Link> <Description>外部链接附件</Description> <Type>URL</Type> </Attachment>
对应Python代码示例
import requests URL = "https://some.almserver.com/qcbin/rest/domains/DOMAIN/projects/PROJECT/runs/<run-id>/attachments" HEADERS = { 'cache-control': "no-cache", "Slug": "external-report.pdf", 'Content-Type': "application/xml", 'Accept': "application/xml" } payload = """<Attachment> <FileName>external-report.pdf</FileName> <Link>https://external-server.com/files/report.pdf</Link> <Description>外部链接附件</Description> <Type>URL</Type> </Attachment>""" def attach_external_url(): response = requests.post(URL, headers=HEADERS, cookies=cookies, data=payload) print(response.status_code) print(response.content)
关键说明
<Type>字段必须设为URL,告知ALM这是外部链接附件,而非本地文件<Link>字段填写目标外部文件的完整URL<FileName>和Slug的值建议保持一致,确保ALM界面显示统一
内容的提问来源于stack exchange,提问作者Arunkumar G
相关产品推荐
相关产品推荐

