我正在尝试使用REST API将附件上传到Azure DevOps中的工作项 . 但是,虽然我可以将附件“上传”并附加到工作项,但是在UI和我下载时,附件总是大小为0KB .
API看起来相当简单,我没有遇到过我使用过的其他几十种API的问题 . 我无法弄清楚这出错的地方 . 这是我正在使用的代码:
import os
import sys
import requests
_credentials = ("user@example.com", "password")
def post_file(url, file_path, file_name):
file_size = os.path.getsize(file_path)
headers = {
"Accept": "application/json",
"Content-Size": str(file_size),
"Content-Type": "application/octet-stream",
}
request = requests.Request('POST', url, headers=headers, auth=_credentials)
prepped = request.prepare()
with open(file_path, 'rb') as file_handle:
prepped.body = file_handle.read(file_size)
return requests.Session().send(prepped)
def add_attachment(path_to_attachment, ticket_identifier):
filename = os.path.basename(path_to_attachment)
response = post_file(
f"https://[instance].visualstudio.com/[project]/_apis/wit/attachments?uploadType=Simple&fileName={filename}&api-version=1.0",
path_to_attachment,
filename
)
data = response.json()
attachment_url = data["url"]
patch_result = requests.patch(
f"https://[instance].visualstudio.com/[project]/_apis/wit/workitems/{ticket_identifier}?api-version=4.1",
auth=_credentials,
headers={
"Accept": "application/json",
"Content-Type": "application/json-patch+json",
},
json=[
{
"op": "add",
"path": "/relations/-",
"value": {
"rel": "AttachedFile",
"url": attachment_url
},
}
]
)
print(patch_result)
print(patch_result.text)
add_attachment(sys.argv[1], sys.argv[2])
我've tried setting/removing/varying every possible header value I can think of. I'尝试使用 requests
中 post
方法的 files
属性(但因为它设置了Content-Disposition而放弃了它,但我使用了所有这些例子),我已经尝试了所有我能想到的,但没有做任何事情任何差异 .
我仍然看到0kb的结果 .
在这一点上我几乎没有想法,所以如果有人知道我可能会出错的地方,我将不胜感激!
1 回答
答案并不明显 . 这是第二次将附件链接到有bug的工作项 . 如果评论没有正确链接 . 即此代码:
本来应该:
如果您未在链接阶段指定注释,则不会记录,也不会期望您获得0KB附件上载 . 没有其他链接类型需要评论 . 我将用文档维护者提出这个问题 .