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

API REST post in C#


#1

HI,

I hope someone can help me with my lousy C# knowledge.

I like to write a C# program to send a POST request to test rail and I fail miserably.

string testRailURL = "https://server.testrail.net/index.php?/api/v2/add_result_for_case/" + runID + "/" + testCase;
string authentication = "email:apiToken";
            
var webRequest = (HttpWebRequest)WebRequest.Create(testRailURL);
webRequest.Method = "POST";
webRequest.ContentType = "application/json";

byte[] binaryAuthorization = System.Text.Encoding.UTF8.GetBytes(authentication);
authentication = Convert.ToBase64String(binaryAuthorization);
authentication = "Basic " + authentication;
webRequest.Headers.Add("AUTHORIZATION", authentication);
           
using(var streamWriter = new StreamWriter(webRequest.GetRequestStream()))
{
   string json = "{" +
      		      "\"status_id\":\"" + statusID + "\", " +
            	      "\"comment\":\""+ message +"\", " +
                          "\"elapsed\":\"" + elapsed + "\"" +
            	 "}";
   streamWriter.Write(json);
 }
 
var httpResponse = (HttpWebResponse)webRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
        	var result = streamReader.ReadToEnd();
 }

This fails with a (401) Unauthorized.

Can someone point me to the right usage of authentication etc in C# for testrail ?

Thanks


#2

a classic one :frowning:
I actually had a typo in the email …

its now working


#3

Hi!

Similar to the Java thread, I would recommend using our official C#/.NET binding:

http://docs.gurock.com/testrail-api2/bindings-dotnet

Cheers,
Tobias


#4

you are of course right, I didn’t see the C# binding… thanks


#5

No problem, glad to help!

Cheers,
Tobias