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

お困りですか?

アトラシアン コミュニティをご利用ください。

コミュニティに質問

プラットフォームについて: Cloud のみ - この記事は、 クラウド プラットフォームのアトラシアン製品にのみ適用されます。

目的

You want to add an attachment to a Jira Service Management Cloud using REST API.

環境

Jira Service Management Cloud 

ソリューション

Two (or more) different REST requests are needed in order to add attachment(s) to Jira Service Management issues.

The first one(s) to add the attachments to the Service Management 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 https://{INSTANCE}.atlassian.net/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 https://INSTANCE.atlassian.net/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 https://INSTANCE.atlassian.net/rest/servicedeskapi/servicedesk 


For more information about this please check the documentation.

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 https://{INSTANCE}.atlassian.net/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. 


For further details please see:



Last modified on Mar 21, 2024

この内容はお役に立ちましたか?

はい
いいえ
この記事についてのフィードバックを送信する
Powered by Confluence and Scroll Viewport.