I am writing a Google App Script code that will submit attachments to Jira's API. The user uploads a file and submits, at which point my code should send a request to the API to send an attachment. My code does not raise any errors, but the attachments are not added to the Jira issue. I think it might be how I am formatting the payload? I used the same settings in PostMan and the API call works fine. My code is as follows:
index.html
function formSubmit(){
var form = $("#bugReportRequest")[0];
google.script.run.withSuccessHandler(BugReportSubmitted).submitBugReport(form)
//where form is:
//<form id="bugReportRequest" name="bugReportRequest" action="#">
// <input type="text" class="form-control" id="BugSubmitterName" name="BugSubmitterName">
// <input type="text" class="form-control" id="BugSubmitterEmail" name="BugSubmitterEmail">
//<input type="file" class="form-control" id="BugReportFileUpload" name ="BugReportFileUpload" />
</form>
}
Code.gs
function submitBugReport(data){
var file = data.BugReportFileUpload;
var url = Jira_URL + "rest/api/2/issue/ABC-2/attachments";
var credentials = Utilities.base64Encode(Jira_Email_Address + ":" + Jira_API_Token);
let formdata = {'file' : file };
var header = {
"Content-Type": "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
"X-Atlassian-Token": "no-check",
"muteHttpExceptions": true,
"Authorization": "Basic " + credentials
}
var options = {
"method": "POST",
"headers": header,
"body": formdata,
"redirect": "follow",
"muteHttpExceptions": true
}
var resp;
try {
resp = UrlFetchApp.fetch(url, options );
console.error(url);
console.log(resp.getContentText());
} catch (e) {
console.error('myFunction() yielded an error: ' + e);
console.error(resp.getContentText);
console.error(resp.getResponseCode);
}
}
The response code I get is 200 but resp.getContentText() only prints "[]". When I check ABC-2 there is no attachment added. Any suggestions on how to format the data payload? Or, is there another reason this would happen?