It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Is it possible to upload an attachment to a Jira ticket via the Rest API and an Axios request? Edited

Right now I am able to upload an attachment via Postman but when I try to do it in my Node.js project I get either a 404, 415 or 500 error. I have tried multiple times changing "Content-Type" to different types and adding and taking away headers but nothing seems to work.

Here is the code I have currently and this returns a 500 error: 

const axios = require('axios');const fs = require('fs');
var FormData = require('form-data');
var formdata = new FormData();formdata.append("file"fs.createReadStream("../something.txt"));
const auth = {    username: username,    password: password}
axios.post("https://jira.ipgaxis.com/rest/api/2/issue/1461331/attachments"formdata, {    auth: auth,    headers: {        "Content-Type": "multipart/form-data",        "X-Atlassian-Token": "no-check"    }}).then(function (response) {    console.log(response);}).catch(function (error) {    console.log(error);});

I have also tried clicking Code in postman and copying and pasting that exact code into my project. With that I get a successful 200 status code but the attachment isn't showing up in the issue. 

Here is that code: 

var fs = require("fs");var request = require("request");
var options = { method: 'POST',  url: 'https://jira.ipgaxis.com/rest/api/2/issue/1461331/attachments',  headers:    { 'cache-control': 'no-cache',     Connection: 'keep-alive',     'Content-Length': '585409',     Cookie: 'atlassian.xsrf.token=A38F-68CD-ET7T-LQ4T_d01126d78cbef48b1897e71402a71625d6f2edc3_lin; AWSELB=97E58B57021817D47A66071828711D73B6F86E38E70A320BA71CC21064A19C52314FA745AE4F4CAD425452D5003971A3AF8CEFE4771007037D2825F3C2992B434FC45329DB; JSESSIONID=35440BB7BC39429962887AF36D56E75C',     'Accept-Encoding': 'gzip, deflate',     Host: 'jira.ipgaxis.com',     'Postman-Token': '403e00fa-35e2-42b6-ba6b-b29b8fde3faa,882a64bd-5515-4bad-8ac3-f0675c6eaabb',     'Cache-Control': 'no-cache',     Accept: '*/*',     'User-Agent': 'PostmanRuntime/7.17.1',     Authorization: 'Basic aWFuLmtlbGxlckBtcm0tbWNjYW5uLmNvbTpNY2Nhbm43NzAwIQ==',     'X-Atlassian-Token': 'no-check',     'Content-Type': 'text/html',     'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' },  formData:    { file:       { value: fs.createReadStream("/Users/ian.keller/Desktop/USJ/nightwatch-test/new.html"),        options:          { filename: 'new.html',           contentType: null } } } };
request(optionsfunction (errorresponsebody) {  if (errorthrow new Error(error);
  console.log(error);  console.log(response)});

And here are screenshots of the code to get a better look: 

Capture.PNGCapture1.PNG 

1 answer

1 accepted

0 votes
Answer accepted

This code is what got it working for me. You have to actually make a curl request in your Node project in order to upload an attachment to a Jira ticket. Pretty ridiculous but I couldn't find anything that worked better. To my knowledge it isn't possible with axios.

var exec = require('child_process').exec;var args = ' -D- -u admin:admin -X POST -H "X-Atlassian-Token: no-check" -F "file=@{filename}.html" https://{companyName}/rest/api/2/issue/{issueID}/attachments';exec('curl ' + argsfunction (errorstdoutstderr) {  console.log('stdout: ' + stdout);  console.log('stderr: ' + stderr);  if (error !== null) {    console.log('exec error: ' + error);  }});

Code.PNG 

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Jira

The add-in you’ve been waiting for: Jira Cloud for Excel 🙌

Introducing Jira Cloud for Excel Here at the product integrations team at Atlassian, we are thrilled to announce the new Jira Cloud for Excel add-in! This add-in lets you export Jira data directly ...

935 views 8 25
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you