我正在尝试使用 REST API 将附件上传到 Azure DevOps 中的工作项。但是,虽然我可以“上传”附件并将其附加到工作项,但附件的大小始终为 0KB,无论是在 UI 中还是在我下载它时。
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])
我试过设置/删除/改变我能想到的每一个可能的 header 值。我试过使用 requests 中 post 方法的 files 属性(但放弃了它,因为它设置了 Content-Disposition,但所有示例我已经看到不要使用那个),我已经尝试设置区域路径参数,我已经尝试了我能想到的一切,但没有任何区别。
我什至使用 Fiddler 观察实际网站是如何处理的,然后将 header 复制到 Python 中的新请求并发送,我仍然看到的是 0kb 结果。
在这一点上我几乎没有想法,所以如果有人知道我可能哪里出错了,将不胜感激!
请您参考如下方法:
这个问题的答案并不明显。这是第二次将附件链接到有错误的工作项的调用。如果未指定评论,则无法正确链接。即这段代码:
json=[
{
"op": "add",
"path": "/relations/-",
"value": {
"rel": "AttachedFile",
"url": attachment_url
},
}
]
应该是:
json=[
{
"op": "add",
"path": "/relations/-",
"value": {
"rel": "AttachedFile",
"url": attachment_url,
"attributes": {
"comment": ""
}
},
}
]
这没有记录,如果您没有在链接阶段指定评论,也不希望您上传 0KB 的附件。没有其他链接类型需要评论。我将向文档维护者提出这个问题。
