2

I am trying to upload to JIRA from SF. I am using below code.

   HttpRequest req = new HttpRequest();
   HttpResponse res = new HttpResponse();
   Http http = new Http();
   Blob headerValue = Blob.valueOf(username+':'+password);
   String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
   req.setHeader('Content-Type','multipart/form-data; boundary=--aRandomString_with_signs_or_9879497q8w7r8number--');
   req.setHeader('Authorization', authorizationHeader);
   req.setHeader('X-Atlassian-Token','nocheck');


   String endpoint = 'http://jira.cimm2.com/rest/api/2/issue/TP-140/attachments';
   req.setMethod('POST');
   string message1= '--aRandomString_with_signs_or_9879497q8w7r8number\n';
   message1+= 'MIME-Version: 1.0\n';
   message1+= 'MIME-Type: text/plain';
   message1+= 'Content-Disposition:attachment;name="file"; filename="cust-serv.pdf"; \n';
   message1+= 'Content-Type: application/pdf; charset=UTF-8';

   message1+= 'Content-Transfer-Encoding: binary\n';
   message1+= 'Content-Length: 1200\n';
   message1+= '\n';
   message1+= +'Hello How Are You';
   message1+= '\n\n';
   message1+= '--aRandomString_with_signs_or_9879497q8w7r8number--\n';

   req.setbody(EncodingUtil.urlEncode(EncodingUtil.base64Encode(blob.valueOf(message1)),'UTF-8'));
   req.setEndpoint(endpoint);
   res = http.send(req);

I am able to get status code as 200, but when i see in JIRA no attachments are there.

Can any one help me where i am doing wrong

0

3 Answers 3

1

I've had this issue before - we ended up setting up an email box proxy and just having Salesforce send the files as attachments with the issues to that e-mail and having Jira watch the inbox - this I believe... was related to not getting SFDC to properly make the form post to JIRA to create the issue's attachments

1
  • 1
    This is the approach we took. Just be aware that JIRA automatically unwraps .TXT files and puts them into the comments. We worked around that by renaming any .TXT to .TEXT extension.
    – Scott
    Nov 28, 2015 at 21:50
0

JIRA Attachment POST requires a multi-part post....The HTTPRequest in salesforce does not eaisly support this. However this post has some information on doing this. I am not 100% (as I have not tried it) sure if the implementation of this will get you what you need but it is a start.

Post multipart without Base64 Encoding the body

And a blog discussing this:

http://blog.enree.co/2013/01/salesforce-apex-post-mutipartform-data.html

-1

You probably need to append the file name to the end point url. Like: req.setEndpoint('http://jira.cimm2.com/rest/api/2/issue/TP-140/attachments'+fileName+'.bin');

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .