Download attachment with atlassian-connect-express sample

Hi,

I am trying to download a Jira issue attachment using the atlassian-connect-express sample (JWT security).  The attachment is received successfully but in string format and not binary.

Trying to save jpeg attachment to file system ends up with the jpeg being corrupted and trying to do a base64 encoding crashes due to invalid characters.

Downloding the jpeg attachment url directly from browser works fine... I think he root cause is that the jpeg is coming as a string. How do I download in binary base64?

 

Function code below as well as two attachments. I am stuck so all help is very much appreciated...

app.get('/img', addon.checkValidToken(), function(req, res) { var httpClient = addon.httpClient(req);
httpClient.get({ "headers": { "Content-Type": "image/jpeg", "Accept": "image/jpeg" }, "url": "https://xxxxx.atlassian.net/secure/attachment/10002/download.jpg" //"url": "rest/api/2/attachment/10002"
}, function(err, response, body) { if (err) { console.log(response.statusCode + ": " + err); res.send("Error: " + response.statusCode + ": " + err); } else { console.log('error:', err); // Print the error if one occurred console.log('statusCode:', response && response.statusCode); // Print the response status code
//Enabling this one crash. Probably because not valid characters - only characters in the range from U+0000 to U+00FF, each representing a binary byte with values 0x00 to 0xFF //var encodedData = base64.encode(body);
fs.writeFile("tmp/test.jpeg", body, function(err) { if (err) { console.log(err); } else { console.log("The file was saved!"); res.send(body); } });

} } ); });

 

Regards
Johan

 

Capture1.PNGCapture2.PNG

 

 

 

1 answer

0 vote

Hi @Johan Brodin,

 

I suggest posting this to our developer community forum.

 

Cheers,

Anne Calantog

ok Thx for the replay!

Did not realize that I had posted it to the wrong forum...

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Jun 14, 2018 in Jira Service Desk

How the Telegram Integration for Jira helps Sergey's team take their support efficiency to the bank

...+ reading Fantasy). The same is true for him at the bank he works for: Efficiency is key when time literally equals money. Read on to learn how Sergey makes most of the time he has by...

419 views 2 4
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you