![](/img/trans.png)
[英]Creating a highly customized Work Item in Azure DevOPs through the API using C#
[英]Azure DevOps API creating work item returns 404 error
我正在嘗試使用 Azure API 添加工作任務,但我不斷收到 404 錯誤。 我試圖做一個獲取所有項目並且這工作(所以我的身份驗證令牌工作正常)。 我什至授予了令牌的所有 Azure 權限,但它仍然返回 404 錯誤。
public class Main
{
public static void Main(string[] args)
{
AzureClient ac = new AzureClient();
var task = ac.AddTask();
}
}
public class AzureClient
{
private readonly HttpClient _client;
public AzureClient()
{
_client = new HttpClient()
{
Timeout = TimeSpan.FromSeconds(30)
};
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// ADDED PAT HERE TO CLIENT
}
public async Task AddTask()
{
List<object> task = new List<object>
{
new { op = "add", path = "/fields/System.Title", value = "Test"}
};
string jsonTask = JsonConvert.SerializeObject(task);
string baseUri = "some base uri";
string uri = $"{baseUri}/_apis/wit/workitems/$Task?api-version=5.0";
// RESPONSE HERE RETURNS 404
var response = _client.PostAsync(uri, new StringContent(jsonTask, Encoding.UTF8, "application/json-patch+json")).Result;
}
}
請在您的代碼中使用'application/json-patch+json'
而不是'application/json'
:
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
如果使用 Postman 測試Api會發現錯誤:
Valid content types for this method are: application/json-patch+json.
這樣我們就需要使用application/json-patch+json
希望這會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.