简体   繁体   中英

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. 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.

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:

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

If you use the Postman to test the Api , you will find the error:

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

That's way we need to use the application/json-patch+json

Hope this will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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