I am trying to call the post API(DevOps) for workitem but it shows me status "Bad Request".

If I call some GET API(DevOps) it's working fine. I follow the below link to call POST API:

https://docs.microsoft.com/en-us/rest/api/azure/devops/wit/wiql/query%20by%20wiql?view=azure-devops-rest-5.0

Code:

using (var client = new HttpClient())
                {
                   string content = "{'query': 'Select [System.Id], [System.Title], [System.State] From WorkItems'}";
                    client.BaseAddress = new Uri("http://YYYYY.com:8080/tfs/YY");  //url of your organization
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);
                    var response = client.PostAsJsonAsync(new Uri("http://YYYYY.com:8080/tfs/YY/_apis/wit/wiql?api-version=1.0"), content).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        Console.Write("Success");
                    }
                    else
                        Console.Write("Error");
                }

1 Answers

0
Cece Dong - MSFT On

You may try the following code:

var personalaccesstoken = "xxxxxxxxxx";
var base64Token = Convert.ToBase64String(Encoding.ASCII.GetBytes($":{personalaccesstoken}"));  
using (HttpClient client = new HttpClient())
{
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", base64Token);
    var requestMessage = new HttpRequestMessage(HttpMethod.Post, "http://{tfsserver}:8080/tfs/DefaultCollection/_apis/wit/wiql?api-version=1.0");
    requestMessage.Content = new StringContent("{\"query\": \"select [System.Id], [System.Title], [System.State] from WorkItems\"}", Encoding.UTF8, "application/json");
    using (HttpResponseMessage response = client.SendAsync(requestMessage).Result)
    {
        response.EnsureSuccessStatusCode();
    }    
}