Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to add Attachment to Jira-SD-Issue via Rest-API

jonas futschik September 15, 2020

Good morning erveryone. I am tasked with streamlining the Support-Ticket creation. Part of the requirement is to create attachments on newly opened tickets.

Jira-SDs Rest-API requires the user to first upload a file to a servicedesk as a temporary attachment, and then make it permanent by attaching the file to an issue. In my case, the request to attach the file to an issue intermittendly returns HTTP-Code 500 along with the following message:

{"errorMessage":"An error occurred while processing the attachment. 
Could not save attachment to storage: Source file does not exist /var/atlassian/application-data/jira/caches/tmp_attachments/temp3914246537425063270",
"i18nErrorMessage":
{"i18nKey":"sd.attachment.create.error",
"parameters":
["Could not save attachment to storage: Source file
does not exist /var/atlassian/application-data/jira/caches/tmp_attachments/temp3914246537425063270"]}}

In about 90% of all cases, the request returns statuscode 201 and creates the attachment as expected. Since the AttachmentID is returned from the previous request, my reasoning is that Jira-SD has indeed processed the attachment and assigned it an ID. My questions are:

  1. What is the lifespan of a temporary attachment?
  2. Does Jira-SD limit the number of created attachments per user, per minute?
  3. Does anyone know of a npm-library/package that simplifies interactions with Jira-SD(!!!)s Rest-API?

Thank you very much, i am at a loss.

 

1 answer

1 vote
moofoo
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
October 19, 2020

Hi Jonas,

Thanks so much for your question.

Regarding your questions,

  1. Jira implies 1 hour lifespan on temporary attachments

  2. JSD has no explicit limits on neither REST API calls, nor the number of attachments a user can create

  3. There are no official Atlassian NPM packages that were designed for JSD specifically

That being said, JSD uses Jira internal APIs to create temporary attachments. And when Jira fails to monitor temporary attachment, it throws the exception you have mention. Check out Jira logs for more details and contact Atlassian Support if you require further assistance.

Regarding using JSD REST API on front-end, the way to add temporary attachment is to make a call like this:

$ curl -X POST -d @picture.png http://${JIRA_BASE_URL}/servicedesk/rest/servicedeskapi/servicedesk/1/attachTemporaryFile --header "Content-Type: image/svg+xml" --header "os_username=${USERNAME}" --header "os_password=${PASSWORD}"

Which you can do with Fetch API like this:

const formData = new FormData();

// make sure the formData's field is "file"
formData.append('file', fileData);

fetch(
  `http://${JIRA_BASE_URL}/servicedesk/rest/servicedeskapi/servicedesk/1/attachTemporaryFile`,
  {
    method: 'POST',
    headers: {
      'Content-Type': 'multipart/form-data',
      'Accept': 'application/json',
      'os_username': username,
      'os_password': password,
    },
    body: formData
  }
)
.then(response => response.json())
.then(({ temporaryAttachments }) => temporaryAttachments.map(({ temporaryAttachmentId }) => temporaryAttachmentId));

Then you should use those temporartAttachmentId values to create permanent attachments using the POST /request/${issueKey}/attachment request:

fetch(
  `http://${JIRA_BASE_URL}/servicedesk/rest/servicedeskapi/servicedesk/1/request/${issueKey}/attachment`,
  {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
      'os_username': username,
      'os_password': password,
    },
    body: JSON.stringify({
      temporaryAttachmentIds: [temporaryId1, temporaryId2],
      _public: true,
    })
  }
);

Thank you,

Artem
Jira Service Desk

PS: if this answer is correct, please click the “Accept answer” button

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events