Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs Post attachment to JIRA

I am receiving http POST response OK 200, but I see no file present on JIRA issue. From my research I can understand that it could be some problem with formData I am sending with request. Below is my code:

var newBuffer = new Buffer(req.Payload, 'base64');
var myReadableStreamBuffer = new streamBuffers.ReadableStreamBuffer({
frequency: 10, // in milliseconds.
chunkSize: 2048 // in bytes.
});

// With a buffer
myReadableStreamBuffer.put(newBuffer);
var formData = {
'file': {
'content': myReadableStreamBuffer,
'filename': req.FileName,
'mimeType': req.MimeType //mimeType from JSON
}
};

var options = {
url: 'https://comapny.atlassian.net/rest/api/2/issue/' + req.ReferenceId + '/attachments',
method: "POST",
json: true,
headers: {
'ContentType': 'multipart/form-data',
'Authorization': 'Basic ' + new Buffer(config.jira.jiraUser.userName + ':' + config.jira.jiraUser.password).toString('base64'),
'X-Atlassian-Token': 'nocheck'
},
formData: JSON.stringify(formData)
};

request(options,
function (error, response, body) {
if (error) {
errorlog.error(`Error Message : PostAttachmentToCSMS : ${error}`);
return response.statusCode;
}
else {
successlog.info(`Attachment posted for issue Key: ${req.ReferenceId} ${response.statusMessage}`);
return response.statusCode;
}
});

I can write file from myReadableStreamBuffer, so that seems ok. Please help me to identify the problem. Many thanks!

like image 340
Faiza Iqbal Avatar asked Sep 05 '17 14:09

Faiza Iqbal


People also ask

How do I upload an attachment to rest API Jira?

Solution Using GUI based Tools (Postman):Make sure to include "Content-Type", "X-Atlassian-Token" under "Headers" section with values shared. Make sure to include key named "file" and of type "File" to select the one from system, or "Text" to access files using absolute path/reference under "Body" section.

How do I find the attachment ID in Jira?

To get the ID of an attachment, you have to read the issue for its list of attached files. /rest/api/2/issue/(issue key)? fields=attachment should do it. Attachment ids are all unique, irrespective of the file content and name, you'll need to read every issue that has been affected.


1 Answers

After spending some more time on it, I have found the correct format for formData:

var newBuffer = new Buffer(req.Payload, 'base64');
var formData = {
    file: {
        value: newBuffer,
        options: {
            filename: req.FileName,
            contentType: req.MimeType
        }
    }
};
like image 82
Faiza Iqbal Avatar answered Oct 17 '22 21:10

Faiza Iqbal