简体   繁体   English

Azure DevOps API 创建工作项返回 404 错误

[英]Azure DevOps API creating work item returns 404 error

I am attempting to add a work task using Azure Apis but I keep getting a 404 error.我正在尝试使用 Azure API 添加工作任务,但我不断收到 404 错误。 I attempted to do a get all projects and this works (so my authentication token is working fine).我试图做一个获取所有项目并且这工作(所以我的身份验证令牌工作正常)。 I even granted all Azure Permissions to the token and it still returns a 404 error.我什至授予了令牌的所有 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;
   }
}

Please use the 'application/json-patch+json' instead of the 'application/json' in your code:请在您的代码中使用'application/json-patch+json'而不是'application/json'

 _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

If you use the Postman to test the Api , you will find the error:如果使用 Postman 测试Api会发现错误:

Valid content types for this method are: application/json-patch+json.

That's way we need to use the application/json-patch+json这样我们就需要使用application/json-patch+json

Hope this will help.希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM