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

Java TestRails Api Add Run Error Invalid characters in URI: [/api/v2/index_php?/api/v2/add_run/8]


#1

Do you know how I can fix this invalid character in URI error?
I’m trying to implement the testrails java api bindings with jenkins.
Thanks.

           String uri = "https://abc.testrail.com";
            String [] caseidsarray2 = {12356, 45678, 78901};
	Map data = new HashMap();
	data.put("name", "AutomationPredeploy");
	data.put("include_all", "0");
	data.put("case_ids", caseidsarray2);
	
    String postaddrun = uri+"/index.php?/api/v2/add_run/8";
         postaddrun = URLEncoder.encode(postaddrun, "UTF-8");
	JSONObject r = (JSONObject) client.sendPost(postaddrun, data);

[/api/v2/https://1mainstream_testrail_com/index_php?/api/v2/add_run/8]")
at com.gurock.testrail.APIClient.sendRequest(APIClient.java:216)
at com.gurock.testrail.APIClient.sendPost(APIClient.java:115)
at com.gurock.testrail.TestRails.BeforeRun(TestRails.java:141)


TestRails Integration With Jenkins - Can't Run TestRail APIClient java file because no main method
#2

I think you will have more luck asking on stackoverflow’s java section than here seeing that you need help debugging you code. :slightly_smiling_face:

Also, I can’t see what’s wrong from your snippet to results “[/api/v2/https://1mainstream_testrail_com/index_php?/api/v2/add_run/8]”)" as output url.


#3

Hi Angee,

Thanks for the post! When using TestRail’s API bindings, some steps are already taken care of for you.

You would not need to include the URL of your TestRail instance in the uri variable, or the /index.php?/api/v2/, as these details are handled through the APIClient object. Instead, your code should look as follows:

  String [] caseidsarray2 = {12356, 45678, 78901};
  Map data = new HashMap();
  data.put("name", "AutomationPredeploy");
  data.put("include_all", "0");
  data.put("case_ids", caseidsarray2);
	
  String uri = "add_run/8";
  JSONObject r = (JSONObject) client.sendPost(uri , data);

I hope this is helpful,
Jon


#4

I ran the code you provided, but it gave me this error

com.gurock.testrail.APIException: TestRail API returned HTTP 400(“Invalid or incomplete JSON string in API request.”)
at com.gurock.testrail.APIClient.sendRequest(APIClient.java:216)
at com.gurock.testrail.APIClient.sendPost(APIClient.java:115)
at com.gurock.testrail.TestRails.BeforeRun(TestRails.java:141)

public static APIClient loginTestRails()
{
APIClient client = new APIClient(“https://abc.testrail.com/”);
client.setUser(“ahgdfghgf”);
client.setPassword(“afghdfghfg”);
return client
}

public static void BeforeRun (APIClient client)
{
some other lines here

String caseidsarray2 = {12356, 45678, 78901};
Map data = new HashMap();
data.put(“name”, “AutomationPredeploy”);
data.put(“include_all”, “0”);
data.put(“case_ids”, caseidsarray2);

String postaddrun = “add_run/8”;
JSONObject r = (JSONObject) client.sendPost(postaddrun, data);

}


#5

Hi Angee,

Thanks for the feedback! When I reviewed your code, I focused on the original API error you received.

This new error message indicates the contents of data are not formatted properly.

The issue appears to be the following line:

String [] caseidsarray2 = {12356, 45678, 78901};

Please try changing this to:

int [] caseidsarray2 = {12356, 45678, 78901};

Please keep in mind we wouldn’t typically be able to fully troubleshoot your API script and implementation, but are happy to help with error messages received through TestRail’s API.

Hope this helps,
Jon


#6

Thanks that fixed it plus I also had to change boolean from 0 to false.
Thank you so much for your help and quick reply.
I finally got it to create the test run.
Awesome. TestRails is awesome. :smile: