{"error":"Invalid or incomplete JSON string in API request."}

I received the error when sending request to TestRail API

ro.setRestUrl(‘https://%MY PROJECT%.testrail.io/index.php?/api/v2/update_run/’ + id)

I’m following the tutorial to create connection:

@Keyword
	def update_run(String id, ArrayList<String> array) {
		def slurper = new JsonSlurper()
		def ro = new RequestObject('Update TestRail test run')
		ro.setRestRequestMethod('POST')
		ro.setRestUrl('https://%MY PROJECT%.testrail.io/index.php?/api/v2/update_run/' + id)

		def httpheader = new ArrayList<TestObjectProperty>()
		httpheader.add(new TestObjectProperty('Content-Type', ConditionType.EQUALS, 'application/json'))
		httpheader.add(new TestObjectProperty('Authorization', ConditionType.EQUALS, 'Basic <ENCODED>'))
		ro.setHttpHeaderProperties(httpheader)

		def body ="{'include_all': false,'case_ids': " + array + "}"
		WebUI.comment('body = ' + body)
		ro.setBodyContent(
				new HttpTextBodyContent(body, 'UTF-8', 'application/json'))
		println ro.getHttpBody()
		def response = WS.sendRequest(ro)
		println response.getResponseText()
		return slurper.parseText(response.getResponseText())
	}
def response = CustomKeywords.'myPackage.get_tests'(GlobalVariable.G_testrail_run_id)

WebUI.comment('response=' + response.toString())

def tcs_to_add = []

for (def n : (0..response['id'].size-1)) {
    if ((response['case_id'])[n].toString() != 'null') {
        tcs_to_add.add((response['case_id'])[n].toString())
    }
}

WebUI.comment(tcs_to_add.toString())

def total_tcs_to_update = []

for (def n : (0..GlobalVariable.G_run_testrail_tc_id.size)) {
    if (GlobalVariable.G_run_testrail_tc_id[n].toString() != 'null') {
        total_tcs_to_update.add(GlobalVariable.G_run_testrail_tc_id[n].toString())
    }
}

for (def n : (0..tcs_to_add.size-1)) {
    if ((tcs_to_add[n]) != 'null') {
        total_tcs_to_update.add(tcs_to_add[n].toString())
    }
}

println total_tcs_to_update
response = CustomKeywords.'myPackage.update_run'(GlobalVariable.G_testrail_run_id, total_tcs_to_update)

WebUI.comment('response=' + response.toString())

String request = '{"results": ['

def status_id

for (def n : (0..GlobalVariable.G_run_testrail_tc_id.size)) {
    if ((GlobalVariable.G_run_testrail_tc_id[n]) != 'null') {
        if ((GlobalVariable.G_run_testrail_tc_status[n]) == 'PASSED') {
            status_id = '1' //passed
            //retest
        } else {
            status_id = '4'
        }
        
        request = request.concat(((('{"case_id":' + (GlobalVariable.G_run_testrail_tc_id[n])) + ',"status_id":') + status_id) + 
            ',"comment":"AT"},')
    }
}

request = request.substring(0, request.length() - 1 //removing last excessive comma from request
    )

request = request.concat(']}')

request = request.replaceAll('null,', '')

request = request.replaceAll('null', '')

WebUI.comment('request=' + request.toString())

println request
response = CustomKeywords.'myPackage.add_results'(GlobalVariable.G_testrail_run_id, request)

WebUI.comment('response=' + response.toString())

Thanks for help.

Hi Eric_Hung,
To prevent Json syntax error in your own code, please try JsonOutput.
In your case, the body variable should be:

JsonOutput.toJson({'include_all': false,'case_ids': array})
2 Likes

@duyluong Many thanks to you and your elegant solution:)

Just FYI - we’ve built a TestRail integration plugin for Katalon Studio. If you have any feedback or suggestions we are happy to hear from you.

https://store.katalon.com/product/13/TestRail-Integration