Nodejs Post attachment to JIRA
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 } }};
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;};
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); });