How to add an attachment to a Jira Service Management ticket using the REST APIs


Platform Notice: Data Center - This article applies to Atlassian products on the Data Center platform.

Note that this knowledge base article was created for the Data Center version of the product. Data Center knowledge base articles for non-Data Center-specific features may also work for Server versions of the product, however they have not been tested. Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.

*Except Fisheye and Crucible

Purpose

You want to add Attachment to the Jira Service Management Issues using REST API

Environment

Jira Service Management Data Center 

Solution

Adding attachment(s) to Jira Service Management issues using the REST API is a two step process.

Step 1- The first part is to add one or more temporary attachments, which can later be converted into permanent attachments by calling the REST API from Step 2.
On successful execution, this resource will return a list of temporary attachment IDs, which are then used in the next calls to convert the attachments into permanent attachments.

The first one(s) is to add the attachments to the Service Management by calling the endpoint: servicedesk/{serviceDeskId}/attachTemporaryFile (2 requests to add 2 files in the below example):

REQUEST:
curl -D- -u {USER}:{PASSWORD} -H "X-Atlassian-Token: nocheck" -H "X-ExperimentalApi: true" -F "file=@{/path/to/file/test2.txt}" -X POST <JSM Base URL>/rest/servicedeskapi/servicedesk/{SERVICE_DESK_ID}/attachTemporaryFile
....
HTTP/1.1 100 Continue
.....
HTTP/1.1 201 Created
......
 
RESPONSE:
{"temporaryAttachments":[{"temporaryAttachmentId":"5ad41f7b-882a-4f12-8a08-9b401d6dd3bf","fileName":"test2.txt"}]} 


REQUEST:
curl -D- -u {USER}:{PASSWORD} -H "X-Atlassian-Token: nocheck" -H "X-ExperimentalApi: opt-in" -F "file=@{/path/to/file/test3.txt}" -X POST <JSM Base URL>/rest/servicedeskapi/servicedesk/{SERVICE_DESK_ID}/attachTemporaryFile

RESPONSE:
{"temporaryAttachments":[{"temporaryAttachmentId":"3f9ff4f9-99a0-4ee0-868e-8b93c8c67bee","fileName":"test3.txt"}]}  

To obtain the {SERVICE_DESK_ID} either go to the browser, open the item that you want to update, and look at the URL for the ID, or run a get request to get the available ID(s) like the following example:

GET <JSM Base URL>/rest/servicedeskapi/servicedesk 

For more information about this please check the documentation.

Step 2- The second request is to add the temporary files attached to the Service Management at the previous step to a ticket by calling the endpoint: /rest/servicedeskapi/request/{issueIdOrKey}/attachment:

REQUEST:
curl -D- -u {USER}:{PASSWORD} -H "Accept: application/json" -H "Content-Type: application/json" -H "X-ExperimentalApi: opt-in" -d '{"additionalComment": {"body": "BODY OF THE COMMENT, IF ANY"}, "public": "true","temporaryAttachmentIds": ["5ad41f7b-882a-4f12-8a08-9b401d6dd3bf", "3f9ff4f9-99a0-4ee0-868e-8b93c8c67bee"]}' -X POST <JSM Base URL>/rest/servicedeskapi/request/{ISSUE_ID}/attachment
 
RESPONSE:
{"comment":{"_expands":["attachment","renderedBody"],"id":"10504","body":"BODY OF THE COMMENT, IF ANY: adding 2 attachments\n\n[^test2.txt] _(0.0 kB)_\n\n[^test3.txt] _(0.0 kB)_","public":true,...............} 

The arguments to be passed with the request are: 

    1. "public": Whether the attachment should be public (visible on the portal for the customer) or not. The accepted values are true or false. Mandatory

    2. "temporaryAttachmentIds": The ID(s) of the temporary file(s) received in the response(s) to the previous request(s)

    3. "additionalComment" : {"body": ".........."}: In case you want to add a comment you can add a string as the comment body. Otherwise you can just avoid using this block. This is not mandatory. 


Last modified on Mar 27, 2024

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.