How to get a attachment in binary with atlassian-connect-express (coming as string...)

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.

Downloading the jpeg attachment url directly from browser works fine… Getting it in a binary format should be a easy thing but I am stuck so any help is highly appreciated…

Below is the function used

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"
    }, function (err, response, body) {

        if (err) {
            console.log(response.statusCode + ": " + err);
            res.send("Error: " + response.statusCode + ": " + err);
        } else {
            console.log('error:', err);

            //Enabling below line  crash due to invalid characters should be in range from U+0000 to U+00FF 
            //var encodedData = base64.encode(body);

            //The file is saved however it is corrupted.... The received body is a string...
            fs.writeFile("tmp/test.jpeg", body, function (err) {
                if (err) {
                    console.log(err);
                } else {
                    console.log("The file was saved!");
                    res.send(body);
                }
            });
        }
    });
});
1 Like

Did you ever find a solution to this? I’ve got the same issue.

Anyone solved this problem? I’m encountering them right now.

I solved this problem. Please try following

httpClient.get("rest/api/content/229598/child/attachment/att33248/download", function (apiErr, apiRes, apiBody) {
			if (!apiErr && apiRes && apiRes.statusCode == 200) {
				res.statusCode = 302;		
				res.setHeader("location", "https://" + apiRes.client._host + apiRes.req.path);
				res.send(apiBody);
			} else {
				console.log("error in downloading resource");
			}
		});