"Response status code does not indicate success: 500 (Internal Server Error)" while creating Test Suite through TFS Rest API

Sudharsan G picture Sudharsan G · Nov 24, 2017 · Viewed 16.3k times · Source

While trying to create a Test Suite using TFS 2017 REST API, I am getting the error:

System.Net.Http.HttpRequestException - Response status code does not indicate success: 500 (Internal Server Error)

Code I tried:

using (var client = new HttpClient())
{
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    string base64StringPat = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", Configs.Pat)));
    AuthenticationHeaderValue authHeader = new AuthenticationHeaderValue("Basic", base64StringPat);
    client.DefaultRequestHeaders.Authorization = authHeader;

    string url = "http://vmctp-tl-mtm:8080/tfs/DefaultCollection/SgkProject/_apis/test/Plans/7/Suites/8?api-version=1.0";
    var content = new StringContent("{\"suiteType\":\"StaticTestSuite\",\"name\":\"Module1\"}", Encoding.UTF8, "application/json");
    using (HttpResponseMessage response = client.PostAsync(url, content).Result)
    {
        response.EnsureSuccessStatusCode();
        string responseBody = await response.Content.ReadAsStringAsync();
        Console.WriteLine(responseBody);
    }
}

I have used this documentation from Microsoft to call the API: Create a test suite

Please guide me in fixing the issue.

Answer

Andy Li-MSFT picture Andy Li-MSFT · Nov 27, 2017

HTTP code 500 means that this is an error on your server. The server threw an exception when trying to process this POST request.

So, this error has nothing to do with HttpClient. Just check your server first and see what causes the exception.

A possibility is that the specified content type is not expected by the server. POST a StringContent will set the content type to text/plain. You might find the server doesn't like that. In this case just try to find out what media type the server is expecting and set the Headers.ContentType of the StringContent instance.

Whatever, I can create the suite by below sample, you can have a try for that:

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace CreateTestSuite
{
    class Program
    {
        public static void Main()
        {
            Task t = CreateTestSuite();
            Task.WaitAll(new Task[] { t });
        }
        private static async Task CreateTestSuite()
        {
            try
            {
                var username = "username";
                var password = "password";

                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Accept.Add(
                        new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                        Convert.ToBase64String(
                            System.Text.ASCIIEncoding.ASCII.GetBytes(
                                string.Format("{0}:{1}", username, password))));

                    string url = "http://server:8080/tfs/DefaultCollection/LCScrum/_apis/test/plans/212/suites/408?api-version=1.0";
                    var content = new StringContent("{\"suiteType\":\"StaticTestSuite\",\"name\":\"Module3\"}", Encoding.UTF8, "application/json");
                    using (HttpResponseMessage response = client.PostAsync(url, content).Result)
                    {
                        response.EnsureSuccessStatusCode();
                        string responseBody = await response.Content.ReadAsStringAsync();
                        Console.WriteLine(responseBody);
                    }

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}