Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Not able Post attachment using rest api from one issue to another issue using script runner cloud

Yogi Patel November 22, 2024

Able get the attachment in source issue using

copyattachments =   issue.fields.attachment

this give 

 

[thumbnail:https://company.atlassian.net/rest/api/2/attachment/thumbnail/155060, filename:email2.PNG, size:7835, author:[accountId:627ee641694943006f36cc78, emailAddress:test@company.net, avatarUrls:[48x48:https://secure.gravatar.com/avatar/7533eb0d6b1a82b4c19ad20348ee8277?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FYP-2.png, 24x24:https://secure.gravatar.com/avatar/7533eb0d6b1a82b4c19ad20348ee8277?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FYP-2.png, 16x16:https://secure.gravatar.com/avatar/7533eb0d6b1a82b4c19ad20348ee8277?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FYP-2.png, 32x32:https://secure.gravatar.com/avatar/7533eb0d6b1a82b4c19ad20348ee8277?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FYP-2.png], displayName:John Doe, accountType:atlassian, self:https://company.atlassian.net/rest/api/2/user?accountId=627ee641694943006f36cc78, active:true, timeZone:America/Los_Angeles], created:2024-11-22T06:45:04.438-0800, self:https://company.atlassian.net/rest/api/2/attachment/155060, id:155060, mimeType:image/png, content:https://company.atlassian.net/rest/api/2/attachment/content/155060]

 

then try in to post this attachment in new created issue

 

def resp2 = post("/rest/api/2/issue/TTEST-123}/attachments")
       .header("Content-Type","application/json")
   .body(copyattachments)//
   //fields: [
         // attachment:copyattachments
                                               
    //  ])
    .asJson()

 

it is given error 

 

POST request to /rest/api/2/issue/TTEST-123/attachments returned an error code: status: 415 - Unsupported Media Type


Please advise

1 answer

1 accepted

0 votes
Answer accepted
Yogi Patel November 25, 2024
Below solution worked. Thanks @Lukasz Grobelny 
if(selectedFields.contains("Att")){
for(int i = 0i < attachment.size(); i++ ){
def attId = attachment.id[i]
def att = Unirest.get('/rest/api/2/attachment/' + attId).asObject(Map).body
def downloadUrl = attachment.content[i]
def readFileResp = Unirest.get(downloadUrl).asBinary()
log.warn(downloadUrl)
def response = Unirest.post('/rest/api/2/issue/' + createdIssueKey + '/attachments')
.header("Accept""application/json")
.header("X-Atlassian-Token""nocheck")
.field("file"readFileResp.bodyatt.title.toString())
.asObject(Map)

 

}
}

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Product Admin
TAGS
atlassian, atlassian government cloud, fedramp, webinar, register for webinar, atlassian cloud webinar, fedramp moderate offering, work faster with cloud

Unlocking the future with Atlassian Government Cloud ☁️

Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.

Register Now
AUG Leaders

Atlassian Community Events