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 votes

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
Community showcase
Published Jan 08, 2019 in Jira

How to Jira for designers

I’m a designer on the Jira team. For a long time, I’ve fielded questions from other designers about how they should be using Jira Software with their design team. I’ve also heard feedback from other ...

1,295 views 5 10
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