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

Having problems attaching an image using Testrail API


Was wondering if anyone could help me attach a file using Testrail API.

Current setup using Eclipse, Java, and using a hashmap inside an arraylist to send my results.

Below is the code:

client.sendPost(“add_plan_entry/”+ plan, CreateTestPlan );
long run_id = (convertJSON);
CreateTestPlan.put(“results”, testResults);

//Where the attachment is sent
client.sendPost(“add_attachment_to_result/” + run_id+"/"+“FileName”);

The code runs with no errors but the file is not attached.

Hi @lawcrens,
sometimes it is hard to follow such code snipplets, but do you really try to add an attachment to a run? Add_attachment_to_result expects a result_id, but you pass the created run_id (OK, TestPlanEntry Id).
Probably I’m wrong, but if the newly created run_id equals an existing result_id as part of an open run, you might not get an error. I’m not sure if it will work in this way (haven’t tested myself), but check to use the result_id.

I tried adding my result_id as well and it is still not attaching the file with no errors. The run_id and result_id are different.

Did you try to add an attachment to an existing result and run?
Means, not create a new run and result before.
Hiow does your path to file looks like?
Did you debug the APIClient?

Just some points I would check…

Here’s the method I use. This is after I post the result (and I can get a result_id so I can add the image to the comments). I use the codeine package for using the TestrailAPI, but it doesn’t support images yet I think.

    private synchronized void addImageToResult(int testCaseId, int runId) throws IOException, APIException {

    APIClient client = getTestRailClient();

    JSONArray testCaseArray = (JSONArray) client.sendGet("get_results_for_case/" + runId + "/" + testCaseId);
    long resultId = (Long) ((JSONObject) testCaseArray.get(0)).get("id");
    long testId = (Long) ((JSONObject) testCaseArray.get(0)).get("test_id");

    try {
//This method gets the screenshot that I have stored in /target
        String imageUrl = getImageFromTarget(testCase, Thread.currentThread().getId());

        //Posts the image as an attachment to the result
        Object imageObject = client.sendPost("add_attachment_to_result/" + resultId,

        //Adds a comment on the testcase (not a result) with the image displayed instead of attached
        String imageId = ((JSONObject) imageObject).get("attachment_id").toString();
        JSONObject commentBody = new JSONObject();
        //This is the markup to display the image in the comment instead of just the attachment
        commentBody.put("comment", "![](index.php?attachments/get/" + imageId + ")");
        client.sendPost("add_result/" + testId, commentBody);

    } catch (FileNotFoundException fileError) {
        client.sendPost("add_result/" + testId,
                new JSONObject().put("comment", "Could not retrieve Image\n" + fileError));
    } catch (IOException | APIException e) {