Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

post attachment in jira via jira rest api , using nodejs

niwar
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 30, 2018
HI everybody,
I have this code, i dont know what is the error, please help me!!!!


module
.exports.add_defect_attachment = function(toolinfo, defectid, filename, filedata, callback)
{
var attachmentdata, tempDirPath, filePath;
var onFileWritten = function(err) {
if (err) {
var res = {};
res.statusCode = 504;
res.statusMessage = 'Cannot write file in temp directory';
callback(res, {});
}
else {

var req = toolinfo.req_header + '/issue/' + defectid + '/attachments';
console.log('req '+req);
toolinfo.cnx.request('POST', req, null, { 'file' :'@'+filePath }, function(res, jsonData) {
fs.unlinkSync(filePath);
callback(res, jsonData);
});

}
}

if (typeof filedata == 'string') {
attachmentdata = new Buffer(filedata, 'binary');
} else{
attachmentdata = filedata;
}
tempDirPath = path.join(__dirname,'..', 'temp');
if (!fs.existsSync(tempDirPath)) {
fs.mkdirSync(tempDirPath);
}
filePath = path.join(tempDirPath, filename);
/* var data =JSON.stringify( {
name: 'fom',
filename : filePath
});
*/



console.log('dataaa '+data);


if (!fs.existsSync(filePath)) {
fs.writeFile(filePath, attachmentdata, "binary", onFileWritten);
} else
{
onFileWritten();
}

}




I always get this error
error: uncaughtException: Unexpected token < in JSON at position 0

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events