Search code examples
c#azure-devopsazure-devops-rest-api

Azure DevOps API - Query Package Metrics returns BadRequest


Trying to make a API call

https://learn.microsoft.com/en-us/rest/api/azure/devops/artifacts/artifact-details/query-package-metrics?view=azure-devops-server-rest-6.0

It says the payload takes a string array of package ids. What exactly are the IDs? I made this call Get Packages which has IDs for the packages and got the one I need (I believe).

Using C#, I made this call after getting that ID from the previous call.

// Make call
var payload = new string[] { "some guid ID" };

var response = await client.PostAsync("/_apis/packaging/Feeds/{feedId}/packages?api-version=6.0-preview.1", new StringContent(JsonConvert.SerializeObject(payload)), Encoding.UTF8, "application/json"));

The response returns BadRequest


Solution

  • The response returns BadRequest

    The cause of the issue is that the json payload content has issue.

    We can add the following json payload sample to the Rest API Request Body:

    {"packageids":["PackageID1","PackageID2"]}
    

    We can get the packages IDs with the Rest API: Artifact Details - Get Packages

    We can get the ID value in the Rest API response.

    Updated:

    var client = new HttpClient();
    var request = new HttpRequestMessage(HttpMethod.Post, "https://{instance}/{collection}/{project}/_apis/packaging/Feeds/{feedId}/packagemetricsbatch?api-version=6.0-preview.1");
    request.Headers.Add("Authorization", "Basic Base64PAT");
    var content = new StringContent("{\"packageids\":[\"88d21b03-de05-4d64-bf10-c15735d1abb6\",\"9cf8e392-84d0-4417-bdf9-482bb34e68a2\"]}", null, "application/json");
    request.Content = content;
    var response = await client.SendAsync(request);
    response.EnsureSuccessStatusCode();
    Console.WriteLine(await response.Content.ReadAsStringAsync());