Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,369,840
Community Members
 
Community Events
168
Community Groups

REST POST to add attachment gives status 200 OK but no attachment is added to the issue (Cloud)

Greetings,

I'm trying to copy an attachment from a Jira-Software issue to a Jira-ServiceDesk issue (and the other direction as well) by using the ScriptRunner addon (script listener).

Basically I have the "linked issue key" on a text field, so when the attachment is added i get the linked issue by searching the issue with the key retrieved from this field and the attachments added from the event, create a multipartentity and use a /rest/api/3/issue/<issueKey>/attachments POST to upload them to the linked issue.

The issue here is, I get a 200 OK status response, but no attachment is added to the linked issue.

So far I'm testing this on a free instance, to be used on a client instance (I believe Standard plan if I'm not wrong)

The full code is as follows:

 

if(user.accountId == "557058:d2e5bd5c-dc49-41eb-a6f0-5e01093666c1"){return} //updated by a user different than the scriptrunner addon user
if(!changelog.items*.field.contains("Attachment")){return} //Only if there're attachments added.

import org.apache.http.entity.mime.MultipartEntityBuilder
import org.apache.http.entity.mime.HttpMultipartMode
import org.apache.http.entity.mime.content.ByteArrayBody

//ID of integrated projects
def jsmPrId = "10001" //Id of the JSM project
def jswPrId = "10000" //Id of the JSW project
//ID of customfields
def linkCfId = "customfield_10054" //id of the Linked Issue Key custom field

def linkedKey = get("/rest/api/3/issue/${issue.key}?fields=${linkCfId}")
.header('Content-Type', 'application/json')
.asObject(Map).body.fields[linkCfId]

def attachment
def fileBody
def multipart
for(i in changelog.items){
if(i.field == "Attachment" && i.to){
attachment = get("/rest/api/3/attachment/${i.to}")
.header('Content-Type', 'application/json')
.asObject(Map).body

fileBody = get("${attachment.content.substring(attachment.content.indexOf('/secure'))}").asBinary().body

multipart = MultipartEntityBuilder
.create()
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
.addPart(
"file",
new ByteArrayBody(
new byte[fileBody.available()],
attachment.filename.toString()
)
)
.build()

post("/rest/api/3/issue/${linkedKey}/attachments")
.header('Content-Type', "${multipart.properties.contentType.value}")
.header('X-Atlassian-Token', 'no-check')
.body(multipart)
.asObject(Map)//.body
}
}

 

Any ideas of why this is not working are more than welcome.

Also, testing this on Script Console (for a single attachment) gives a statuscode 200 OK and no attachment is added to the linked issue.

stats200OK.png

Actually there's another thing about this, if the attachment is bigger than certain contentLength it gives a contentLength too high error (I didn't measure this, basically it did throw the error with a .png file, but not with a test .pdf and a test .txt files)

Thanks in advance, kind regards,

Alvaro.

3 answers

0 votes

I found in another answer that you have to set the name of the body part to the string "file". This works for me.

0 votes

I have this same issue. Very annoying, please fix.

0 votes

Hi @Alvaro Jardón 

It seems that the body is not valid for your API call and that is the reason why you are not to create the attachment. You will need to write your own custom script which gets the details of the attachment from the source issue and then calls the Add attachment API which is documented here to add each attachment to the required issue to copy it over.

Hope this will help you.

Thank you. 

Kind Regards

Kate

Hi Kate,

Actually that's what the script I posted does, get the attachment and call the add attachment api...

Also, how is it I get a 200 status code if it's not valid?

Thanks, kind regards,

Alvaro.

It is unacceptable that Jira does not return an error status if it is not going to accept the request and act on it.

I'm having similar problems where .zip files are silently rejected by Jira. 

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events