Add attachment using API JIRA

Afifa April 12, 2023

I am trying to add attachment by using the two API POST /rest/servicedeskapi/servicedesk/{serviceDeskId}/attachTemporaryFile and servicedeskapi/request/{issueIdOrKey}/attachment

I use a popup to upload the attachement and this is How i retrieve it

let file =document.getElementById(idElement);
let formData = new FormData();
formData.append('file', file.files[0]);

and then i pass it to this function:

function addAttachment(keyOrId, comment, formData) {
const requestData = {
"temporaryAttachmentIds": [],
"public": true,
"additionalComment": {
"body": comment
}
};

AJS.$.ajax({
url: restUrl,
type: "POST",
processData: false,
contentType: "multipart/form-data",
headers: {
"X-Atlassian-Token": "no-check",
"X-ExperimentalApi": "opt-in"
},
data: file,
success: function(response) {
// The file was uploaded successfully, so attach it to the issue
const attachmentId = response.temporaryAttachmentId;
requestData.temporaryAttachmentIds.push(attachmentId);

AJS.$.ajax({
url: attachurl,
type: "POST",
contentType: 'application/json; charset=UTF-8',
headers: {
"X-Atlassian-Token": "no-check",
"X-ExperimentalApi": "opt-in"
},
data: JSON.stringify(requestData),
success: function(response) {
// The file was attached to the issue successfully
console.log("File attached to issue successfully");
},
error: function(jqXHR, textStatus, errorThrown) {
console.error(`Error attaching file to issue: ${errorThrown}`);
}
});
},
error: function(jqXHR, textStatus, errorThrown) {
console.error(`Error uploading file: ${errorThrown}`);
}
});
}

 

but it does not work and does not show any errorr. Anyone could tell me what I am doing wrong?

0 answers

Suggest an answer

Log in or Sign up to answer