Download attachment with atlassian-connect-express sample

Johan Brodin October 27, 2017

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
Anne Calantog
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
November 5, 2017

Hi @Johan Brodin,

 

I suggest posting this to our developer community forum.

 

Cheers,

Anne Calantog

Johan Brodin November 18, 2017

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