11

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!

Faiza Iqbal
  • 191
  • 2
  • 11

3 Answers3

5

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
        }
    }
};
Joon
  • 45
  • 8
Faiza Iqbal
  • 191
  • 2
  • 11
  • This is fantastic. Note that you do not need to use `JSON.stringify` on your formData property when using nodes request module. Thanks! – Cory Kleiser Feb 12 '20 at 13:00
0

For whom like me getting errors with this API.

After struggling so many hrs on this thing, I finally found this works like a charm. I've got "XSRF check failed" 403/404 error message before writing this code.

// Don't change the structure of formData.
const formData = {
    file: {
        value: fs.createReadStream(filepath),
        options: {
            filename: filename,
            contentType: "multipart/form-data"
        }
    }
};

const header = {
    "Authentication": "Basic xxx",
    // ** IMPORTANT **
    // "Use of the 'nocheck' value for X-Atlassian-Token
    // has been deprecated since rest 3.0.0.
    // Please use a value of 'no-check' instead."
    "X-Atlassian-Token": "no-check",
    "Content-Type": "multipart/form-data"
}

const options = {
    url: "http://[your_jira_server]/rest/api/2/issue/[issueId]/attachments",
    headers: header,
    method: "POST",
    formData: formData
};

const req = request(options, function(err, httpResponse, body) {
    whatever_you_want;
};
Joon
  • 45
  • 8
-1

I was able to post attachments to JIRA using axios in the following way:

const axios = require('axios');
const FormData = require('form-data')
const fs = require('fs');

const url = 'http://[your_jira_server]/rest/api/2/issue/[issueId]/attachments';

let data = new FormData();
data.append('file', fs.createReadStream('put image path here'));

var config = {
    method: 'post',
    url: url,
    headers: {
        'X-Atlassian-Token': 'no-check',
        'Authorization': 'Basic',
        ...data.getHeaders()
    },
    data: data,
    auth: {
        username: '',
        password: ''
    }
};

axios(config)
    .then(function (response) {
        res.send({
            JSON.stringify(response.data, 0, 2)
        });
    })
    .catch(function (error) {
        console.log(error);
    });
Bestin John
  • 1,765
  • 1
  • 20
  • 23