2

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;
   }
}
10
  • 1
    What is the baseUri variable set to? Look at the uri variable and ensure it's correct. Also, you should await the call to PostAsync to avoid deadlocks. Commented Apr 22, 2021 at 16:44
  • @DanielMann I am going to fix the async part thanks. The base uri I can confirm works since I tested the following url and it returns a list of projects under my organization "baseUri/_apis/projects?api-version=5.0" Commented Apr 22, 2021 at 16:49
  • 1
    The REST API URI needs to contain the project name or ID. As I said, look at the uri variable. Don't assume it's correct. Compare that URI to the REST API documentation. Commented Apr 22, 2021 at 16:50
  • 2
    Are you targeting Azure DevOps Server/TFS on-prem or are you targeting Azure DevOps? If you're targeting Azure DevOps, I'd strongly recommend using the latest REST API version, which is 6.1. Beyond that, unless you're willing to provide the exact URI you're trying to access, there's not much more guidance anyone can give you. The 404 error means you're not giving it a valid URI to the API. Commented Apr 22, 2021 at 16:54
  • 2
    Wait a minute. If you're on-prem, then how are you using dev.azure.com? That wouldn't work. You're not providing the complete picture which is going to make it very difficult to help you. Please update your question with the exact URI you're using. Feel free to redact a bit of it for privacy. Also include what version of TFS/Azure DevOps Server you're using. Commented Apr 22, 2021 at 17:36

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

While this isn't what I needed, you are correct on the content type which I noticed after.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.