What is exact JSON format for posting attachment to JIRA?

Faiza Iqbal September 5, 2017

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;
}
});

1 answer

1 accepted

1 vote
Answer accepted
Faiza Iqbal September 6, 2017

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 } } };

Dan Lamb May 7, 2018

@Faiza Iqbal I cannot thank you enough for posting this!

Like Bhanu likes this

Suggest an answer

Log in or Sign up to answer