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

API Attachments. Method for displaying Images

Hello,

Maybe there’s an easier way of doing this but will post my way anyway. Attaching attachments to a result, only shows the file, meaning you have to open the image (if it is an image) in a new tab. My postImage method takes the image ID after I post it and then I add another comment with the correct markdown, thus displaying the image without having to open a new tab.

Post the image, but save the response

Object imageResponse = client.sendPost("add_attachment_to_result/" + resultId, imagePath);

Get the image ID

String imageId = ((JSONObject) imageObject).get("attachment_id");

Create a body, just containing a comment, no results. But use the formatting below to add the imageID

![](index.php?/attachments/get/<imageId>)

JSONObject commentBody = new JSONObject();
commentBody.put("comment", "![](index.php?/attachments/get/" + imageId + ")")

Then sendPost another result, but only containing the comment. It will add a comment to the test with the image displayed

client.sendPost("add_result/" + testId, commentBody)

I’m not a developer, so forgive my potential bad practices in the above. Maybe there’s a simpler way, but maybe this could help someone. If there is a simpler way, please let me know.