Join 34,000+ subscribers and receive articles from our blog about software quality, testing, QA and security.
 

Adding Test Results Through API


#1

I am trying to add test results via the API and am getting a (400): Bad Request error. Does anyone know what might be wrong with this code? It worked on the old API before it required login credentials.

using (var client = new WebClient())
{
string testResultID = “59”;
string BaseURI = “http://testrailurl/index.php?/api/v2/”;
string url = string.Format("{0}add_result/{1}", BaseURI, testResultId);

            string authInfo = "login@email.com:password";
            authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
            client.Headers["Authorization"] = "Basic " + authInfo;

            client.Headers.Add("Accept: application/json");
            client.Headers.Add("Content-Type: application/x-www-form-urlencoded");

            var data = new NameValueCollection();
            data.Add("status_id", testStatus);
            data.Add("comment", comment);

            var response = client.UploadValues(url, "POST", data);
            var str = Encoding.Default.GetString(response);
        }

#2

Hello Drew,

Thanks for your posting. There are more changes between the old and new API and a full migration guide can be found here:

http://docs.gurock.com/testrail-api/migration

The issue in your case is that you don’t submit the data (status_id, comment) as JSON and this results in the 400 Bad Request response. A discussion about JSON libraries for C#/.NET can be found here:

http://stackoverflow.com/questions/1474377/json-library-for-c-sharp

Regards,
Tobias