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

[API] Get an error when trying to add attachment using add_attachment_to_result with Javascript

Hi,

Following the documentation (http://docs.gurock.com/testrail-api2/reference-attachments) I am trying to upload a screenshot to a test result like this:
TestRail.prototype.addCommandAttachments = function(command, id, postData, callback) {
return request.post({
uri: this.getFullHostName() + command + id,
headers: {
“content-type”: “multipart/form-data”
},
body: postData
}, function(err, res, body) {
return callback(body);
}).auth(this.user, this.password, true);
};

TestRail.prototype.addAttachmentToResultsForCases = function(result_id, results, callback) {
return this.addCommandAttachments(“add_attachment_to_result/”, result_id, case_id, JSON.stringify(results), callback);
};

testrail.addAttachmentToResultsForCases(39, “/Users/reports/images/00a300eb-00d3-008d-00c1-002000d700f1.png”, function(results) {
console.log(results);
});

However, I get the “no file upload or the size limit was exceeded” and “Your browser sent a request that this server could not understand.” error message.

could you please help me?

Best,
DatDC

Hi @datdc,

Thanks for the post! The error message you are receiving would likely indicate the body of the POST request is not formatted properly for TestRail’s API to receive.

We would recommend reviewing the source code for one of our existing bindings (Java, for example) for assistance properly formatting the body of the request, as these bindings explicitly define the boundary of the attachment and other details, which may need to be handled in your JavaScript implementation.

Hope this helps,
Jon

Got it, thanks Jon.reynolds!

Hi Jon,

I always got the “No file attached or upload size was exceeded.” error message when adding an attachment to result.

my script:

TestRail.prototype.addCommandAttachments = function(command, id, postData, callback) {
return request.post({
uri: this.getFullHostName() + command + id,
headers: {
“content-type”: “application/json”
},
body: postData
}, function(err, res, body) {
return callback(body);
}).auth(this.user, this.password, true);
};

TestRail.prototype.addAttachmentToResults = function(results_id, attachment, callback) {
var json;
json = {};
json.attachment = attachment;
return this.addCommandAttachments(“add_attachment_to_result/”, results_id, JSON.stringify(json), callback);
};

testrail.addAttachmentToResults(33960, “/Users/sgsrender/Desktop/ScreenShot2.jpg”, function(results) {
console.log(results);
});

Note that when I use the “https://xxxxx.testrail.io/index.php?/attachments/ajax_add_for_project/1&is_upload=1” endpoint to add attachment, the “Maximum POST or upload size was exceeded.” error message appears.

could you please help me?

Best,
DatDC

Hi @datdc,

Thanks for reply, glad to hear you were able to get the API working. In regards to the Ajax endpoint, we wouldn’t be able to troubleshoot this, unfortunately, as this is not a standard attachments API endpoint which we would document externally or have troubleshooting steps for.

We would recommend using the UI or standard TestRail API endpoints where available.

Regards,
Jon

Hi Jon,

I still got the “No file attached or upload size was exceeded.” error message with the " add_attachment_to_result" API.

There is no guide for javascript in the TestRail API document (http://docs.gurock.com/testrail-api2/start). Could you please help me?

Best,
DatDC

HI @datdc,

Thanks for the reply! Here is an example piece of JavaScript which should work for you to submit an attachment to TestRail.

var request = require('request');
var fs = require('fs');

var auth = Buffer.from(user + ":" + pwd).toString('base64');

const options = {
    method: "POST",
    url: "https://{your server}/index.php?/api/v2/add_attachment_to_result/{result_id}",
    port: 443,
    headers: {
        "Authorization": "Basic " + auth,
        "Content-Type": "multipart/form-data"
    },
    formData : {
        "attachment" : fs.createReadStream('./image.jpg')
    }
};

request(options, function (err, res, body) {
    if(err) console.log(err);
    console.log(body);
});

Hope this helps,
Jon

1 Like

Hi Jon.reynolds,

Your example worked well and I’m able to upload images to the results normally, thank you so much!

Best,
DatDC

1 Like