Service Desk API - Adding Attachment

Jarrod DeBoy May 1, 2019

We are wanting to add attachments to an existing service desk using the POST API from our React Native application.  We are only receiving back "JSON Parse error: Unexpected EOF" from the API.  here is our post:

const formData = new FormData();
const fileParts = ["Hello Jira!"];
const blob = new Blob(fileParts, {type : 'text/plain'});

formData.append('image', blob, 'file_name.txt' );

await fetch('https://THING.atlassian.net/rest/servicedeskapi/request/{ID}/attachment', {
method: 'POST',
headers: {
'Authorization' : 'Basic BASE64',
'Content-Type': 'multipart/form-data',
'X-Atlassian-Token': 'no-check'
},
data: formData
})

 

1 answer

1 accepted

3 votes
Answer accepted
Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 3, 2019

Hi Jarrod,

Service Desk Cloud does this a bit differently than Jira Core or Jira Software would.  This is because Service Desk has some default restrictions as to what the customer can see via the customer portal.  The endpoint you're trying to POST to is documented in https://developer.atlassian.com/cloud/jira/service-desk/rest/#api-rest-servicedeskapi-request-issueIdOrKey-attachment-post

From that page:

POST /rest/servicedeskapi/request/{issueIdOrKey}/attachment

This method adds one or more temporary files (attached to the request's service desk using servicedesk/{serviceDeskId}/attachTemporaryFile) as attachments to a customer request and set the attachment visibility using the public flag. Also, it is possible to include a comment with the attachments.

To get a list of attachments for a comment on the request use servicedeskapi/request/{issueIdOrKey}/comment/{commentId}/attachment.

So in this case, this endpoint can only add a temporary file that has already been uploaded to this issue.  That endpoint is not where you actually upload the file itself via REST. 

Instead you should first use the POST /rest/servicedeskapi/servicedesk/{serviceDeskId}/attachTemporaryFilePOST endpoint to upload that file.   Once that temp file is there, you need to find the Id of that file (which you will see in the response if uploaded successfully, OR you could call the endpoint GET /rest/servicedeskapi/request/{issueIdOrKey}/attachment to see all temp files on that issue).  From there you can use your other endpoint to post this attachment, choose public as true if the customer is supposed to see this attachment, and add a comment if you like.

The syntax for that in curl would look like this:

curl --request POST \
  --url 'https://your-domain.atlassian.net/rest/servicedeskapi/request/{issueIdOrKey}/attachment' \
  --header 'Accept: application/json' \
  --header 'Content-Type: application/json' \
  --data '{
  "temporaryAttachmentIds": [
    "temp910441317820424274",
    "temp3600755449679003114"
  ],
  "public": true,
  "additionalComment": {
    "body": "Please find the screenshot and the log file attached."
  }
}'

I hope this helps.

Andy

Jarrod DeBoy May 3, 2019

Andy, this is very helpful and we have it already working!  Thanks so much!

raveena April 27, 2020

hey i am getting "sd.attachment.temporary.session.timeout.id" this error - i do get tempid but while uploading attachments it throws this error and if i copy the temp id and upload it through postman it works fine.Strange isn't it

Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 30, 2020

That is very strange to see.  I have not found any other occurrences of this particular error having been reported in any of our support channels.  @raveena I would be interested to learn more about the details of your REST call here.  Such as

  1. How large the file is you are uploading?
  2. What specific headers being sent with your rest call?
  3. How long between the time this request is sent and the time you see this error?  The timeout aspect makes it seem like perhaps the request cannot be completed within defined time period.
  4. You mentioned this working in Postman, what kind of REST client are you using where this is not working? Curl, a connect app, some other rest client?

I am interested to try to learn more here, however we might want to create a new question to address that specific problem.

Andy

Alistair Byrne July 14, 2020

Hey Andy,

I'm looking through the documentation on the Service Desk API page and I'm a little confused. When attaching a temporary file onto the service desk there is no example that shows how to actually give a file path. There is a command line example that includes a file path, but nothing for in app use. Am I missing something?

 

-Alistair

Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 15, 2020

Hi Alistair,

I'm not sure I have a clear answer for that.  I have not built a connect app to perform this action, so it's not clear to me how it might be different.  But perhaps one of the coded solutions on this other thread might help shed some light on it https://community.atlassian.com/t5/Jira-Questions/How-do-I-upload-attachment-to-JIRA-Issue-via-REST-API/qaq-p/545073

There are a few different examples there, but granted these are using the Jira Core REST APIs to upload files to Issues.  It's not exactly the same as Service Desk adding such files to requests, but it's very close in nature that perhaps these example might help.

Aymeric Hermant December 11, 2020

Hi Andy,

Thanks for your answers on this topic.
Most of the time, we get the following errors  when we use attachTemporaryFile before attachment :

{‘errorMessage’: ‘An error occurred while processing the attachment. Could not save attachment to storage: Source file does not exist /var/atlassian/jirahomelocal/caches/tmp_attachments/temp1076873519188975551’, ‘i18nErrorMessage’: {‘i18nKey’: ‘sd.attachment.create.error’, ‘parameters’: [‘Could not save attachment to storage: Source file does not exist /var/atlassian/jirahomelocal/caches/tmp_attachments/temp1076873519188975551’]}}

{‘errorMessage’: ‘There was a problem saving the attachments for this comment’, ‘i18nErrorMessage’: {‘i18nKey’: ‘sd.attachment.temporary.session.time.out.ids’, ‘parameters’: []}}

A lot of them disappear if we add a sleep between attachTemporaryFile and attachment but that's an arbitrary solution so that is not very clean.

Is there a way to ensure that the temporary file has been properly uploaded before to call /rest/servicedeskapi/request/{issueIdOrKey}/attachment ?

Katherine Regan
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 9, 2021

Hey @Aymeric Hermant , about how long is your sleep (if you know)? Also, do you know what request status you're getting on the first error call? 400 or 500?

We're also seeing this issue. Most of the time the attachment call works, but sometimes the temporary attachment ids give the message error message you posted.

Thanks

Sukrit Khera September 20, 2023

I am also getting this error on JSM Sandbox,  but the same code works fine on regular JSM,

Suggest an answer

Log in or Sign up to answer