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

Simple "get_results" Demo failed


#1

Hi,

I am trying to do a simple demo to “get_results” of an test case.
I ran the code below, but got “HTTP 301” error. Our Testrail login Url is “https://companyname.testrail.com/index.php”, so what the root url should be set for APIClient url? or something else caused the failure? Thanks!

`
public class TestrailHttpRequest {

public static void main(String[] args) throws Exception {
    
    APIClient client = new APIClient("http://companyname.testrail.com/");
    client.setUser("firstname.lastname@companyname.com");
    client.setPassword("password");

    try{
        JSONObject json = (JSONObject) client.sendGet("get_results/T86082");
        System.out.println(json);
    }
    catch(APIException e){
        e.printStackTrace();
        System.out.println("Error Message is: "+e.getMessage());
    }
}

}
`

It failed with error below:

com.gurock.testrail.APIException: TestRail API return HTTP 301 (No additional error message received)
at com.gurock.testrail.APIClient.sendRequest(APIClient.java:159)
at com.gurock.testrail.APIClient.sendGet(APIClient.java:91)
at TestrailHttpRequest.main(TestrailHttpRequest.java:21)
Error Message is: TestRail API return HTTP 301 (No additional error message received)


#2

The first problem is you included the T in the test id (maybe this works but not in example code). I believe it is implied by the request you are making.

Since you are requesting results you will get a JSONArray of JSONObject types back.

This should work:
JSONArray json = (JSONArray) client.sendGet(“get_results/86082”);

and then iterate over json to output JSONObjects…

for ((JSONObject) obj : json) {
System.out.println(obj)
}


#3

Thank you Jgourlay, after removing the “T” in test id, then code works!

I have two more questions:

  1. I am not the administrator of our Testrail, how can I know which version of Testrail is?
  2. With API “add_result” or “add_result_for_case”, how can I attach file when add test result by API?

Thanks!


#4

Hi Vincent,

Thanks for your posting. You get the 301 response because TestRail is running with https (and not standard http), so the address needs to be: https://companyname.testrail.com/

  1. The version of TestRail is shown in the administration area but also in the help/about dialog (Help & Feedback > About TestRail in the upper right) or on the login screen.

  2. Uploading attachments via the API is not directly possible and we recommend uploading files and screenshots to an external location and then including a link (or embedding the images) as part of the test result comment.

I hope this helps!

Cheers,
Tobias


#5

Hi Tgurock,

Thank you for your response, your suggestions are really helpful, I found the version of the Testrail, and good to know the best way of uploading screenshots by API.

Thanks,
Vincent


#6

You are welcome, Vincent, great to hear that this helps :slight_smile:

Cheers,
Tobias