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

Can't add results for case; invalid or incomplete JSON string in API request error


#1

I can’t add result for case. I’m getting an invalid or incomplete JSON string in API request error. Does anyone know how I can fix this?
Thanks.

long run_id = 123456;
int case_id = 0;
int status_id = 0;

for(Map.Entry<Integer, Integer> entry: map.entrySet()) {
case_id=entry.getKey();
status_id=entry.getValue();

String postaddrun = “add_result_for_case/”+ run_id +"/"+case_id;
JSONObject r = (JSONObject) client.sendPost(postaddrun, “{status_id:”+status_id+"}");

com.gurock.testrail.APIException: TestRail API returned HTTP 400(“Invalid or incomplete JSON string in API request.”)


#2

Hi Angee,

This error message indicates there is a formatting issue with the body used in the API request.

In the working example from your other post, you were submitting data as a Map, which did not yield an error. I would recommend using this consistently, as adding values to the Map and using this as your post body may provide more consistent results than trying to piece together strings in your submission.

Hope this helps,
Jon


#3

Thanks I will try that solution.


#4

It is no longer throwing an error, but the results are not posting into testrails.
Not sure where I went wrong.
Map data = new HashMap();
for(int i=0; i<testrunresultsarray.length; i++)
{
if(testrunresultsarray[i].startsWith(“C”))
{
String testcaseid = testrunresultsarray[i].replace(“C”, “”);
int case_id= Integer.parseInt(testcaseid);
String tcstatusstr = testrunresultsarray[i+1];
Reporter.log(“test case status is “+tcstatusstr, true);
int tcstatus = Integer.parseInt(tcstatusstr);
data.put(“status_id”, tcstatus);
String postaddrun = “add_result_for_case/”+run_id+”/”+case_id;
Reporter.log("Post add run string is "+postaddrun, true);
JSONObject r = (JSONObject) client.sendPost(postaddrun, data);
}
}


#5

Hi Angee,

If you are not receiving an error from TestRail, but results are not being added, you may need to review your implementation to ensure the request is being made, and also that the actual data being sent follows the format laid out in our API documentation.

If you are able to provide the data actually being sent to TestRail in one of the API requests, we’d be glad to review that for errors.

As an additional note, if you are looking to add multiple results through an iteration like the one in your example, we would recommend using the add_results_for_cases method instead, as this would result in faster processing and fewer API calls.

Thanks,
Jon


#6

I changed it to use add results for cases per your suggestion and now I got it to work.
Thanks.