为什么在尝试向TFS发送REST请求时会出现404错误



我试图通过REST API在本地TFS中创建任务,但总是出现404错误。有人知道我是否使用了正确的URI,正确的URI是什么,或者我是否做错了什么吗?

我学习了关于https://learn.microsoft.com/de-de/rest/api/azure/devops/wit/work%20items/create?view=vsts-rest-tfs-4.1并下载了他们的示例项目。但这些都没有帮我摆脱404。TFS的基本URI是正确的,我可以通过浏览器进行处理。

JsonPatchDocument createStoryRequest = new JsonPatchDocument();
createStoryRequest.Add(
new JsonPatchOperation()
{
Operation = Operation.Add,
Path = "/fields/System.Title",
Value = storyToCreate.Fields.Title
}
);
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", ApiCredentials.tfsAuthenticationToken))));
Task postJsonTask = Task.Run(async () =>
{
using (HttpResponseMessage response = await client.PostAsJsonAsync(
"https://my_tfs_server/tfs/DefaultCollection/_apis/wit/workitems/$task?api-version=4.1",
createStoryRequest))
{
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
System.Console.WriteLine(responseBody);
}
});
Task.WaitAll(new Task[] { postJsonTask});
}

我希望在我的TFS上创建一个故事/任务/任何东西,但无论我尝试什么,我总是得到404。

谢谢你的帮助!

第1版:感谢大家帮助我!解决方案是API版本:TFS2018仅支持API 4.0版,当提供另一个API版本时,将给出所描述的404错误。识别TFS的版本确定API版本并使用以下代码(以及RestSharp NuGet包):

var client = new RestClient("https://my_tfs_server/tfs/DefaultCollection/PROJECTNAME/_apis/wit/workitems/$User Story?api-version=4.0");
client.Authenticator = new HttpBasicAuthenticator("", ApiCredentials.tfsAuthenticationToken);
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("Content-Type", "application/json-patch+json");
request.AddParameter("undefined", "[{"op": "add", "path": "/fields/System.Title", "value": "" + storyToCreate.Fields.Title + "" }]", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

根据API 4.1版的描述,正确的POST命令是:

POST https://{instance}/{collection}/{project}/_apis/wit/workitems/${type}?api-version=4.1

因此,与您的样本相比,我认为您错过了集合后的项目名称。

所以应该是这样的:

using (HttpResponseMessage response = await client.PostAsJsonAsync(
"https://my_tfs_server/tfs/DefaultCollection/PROJECTNAME/_apis/wit/workitems/$task?api-version=4.1",
createStoryRequest))
{
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
System.Console.WriteLine(responseBody);
}

编辑
因此必须调整URL以包含项目名称:"https://my_tfs_server/tfs/DefaultCollection/PROJECTNAME/_apis/wit/workitems/$task?api version=4.1"其中"PROJECTNAME"应该是您团队项目的名称。
如果在没有项目名称的情况下使用,您似乎会得到404,如果您指定了一个不存在的项目,您会得到一些错误,即该项目不存在。

第2版:
根据这篇文章,您的TFS版本与TFS 2018 RTM有关,根据github上的评论,该版本支持4.0版中的REST API
到目前为止使用的示例使用的是api 4.1版本,显然不支持该版本
REST API 4.1之前版本的文档有点隐藏,但此链接应提供正确的规范。它似乎创建了你必须提供一个补丁请求:

PATCH https://{instance}/DefaultCollection/{project}/_apis/wit/workitems/${workItemTypeName}?api-version={version}

请求的主体应该包含JSON格式的字段值:

[
{
"op": "add",
"path": { string }
"value": { string or int, depending on the field }
},
{
"op": "add",
"path": "/relations/-",
"value":
{
"rel": { string },
"url": { string },
"attributes":
{
{ name/value pairs }
}
}
}

]

因此,api版本应该是4.0。

第3版:(通过问题海报)我的中介类型也是错误的。"application/json"将导致"错误请求"响应。正确的媒体类型是"application/json-patch+json"。

相关内容

最新更新