Uploading attachments via REST API using axios

Abdul Zubair June 5, 2017

I'm having a lot of trouble uploading an attachment to a created issue using the REST API. Currently I'm getting a 415 - "unsupported media type" error. 

I would really appriciate some help with this. Thanks! 

 

let actionData=new FormData();
actionData.append('file', files[0]);
actionData.append('comment', "foobar");
actionData.append('minorEdit', "true");
axios({
      method: 'POST',
      url: jiraApiUrl+'/issue/'+issueID+'/attachments',
      headers: {
             'X-Atlassian-Token': 'nocheck',
      },
data: actionData,
}).then(response => {
              //if (!cb) return { error: 'No callback' };
             console.log('response-2', response);
             return ;//cb(response);
})
.catch((err) => console.log('errorTest - files', err));

4 answers

2 votes
Nicholas Gomez March 16, 2022

  I want to post an answer because I've just spent the entire morning figuring this out, and there isn't much discussion on how to do this with axios. Much of the discussion that does exist will suggest to remove the Content-Type header or set it to false, but that did not work for me.

  You can set Content-Type to the 'multipart/form-data' as the docs suggest and then calculate the boundary with the form-data library function FormData.getBoundary(). Here is some sample code that should work:

 

async function testAxiosPost() {

  const fd = new FormData();

  fd.append('file', fs.createReadStream('./file_name.png'));

  const url = 'https://{jira_instance}.atlassian.net/rest/api/2/issue/{issue_key}/attachments';

  await axios({

    method: 'POST',

    url: url,

    headers: {

      'Authorization': 'Basic {token}',

      'X-Atlassian-Token': 'nocheck',

      'Content-Type': 'multipart/form-data; boundary=' + fd.getBoundary()

    },

    processData: false,

    cache: false,

    data: fd

  })

  .then((response) => console.log(response))

  .catch((e) => console.error(e.response));

}
John Henry May 9, 2023

This worked for me. Thank you!

Berkeley Lincicome August 29, 2023

I wanted to upvote this and give it 1000 stars. After so much effort this worked for me as well. 

 

For reference, this is in next js using formidable for the files object and type 

 

async function attachFilesToJiraIssue(issueIdOrKey: string, files: Files, res: NextApiResponse) {
const formData = new FormData();
for (const [key, file] of Object.entries(files)) {

const fileStream = fs.createReadStream(
(file as File).filepath
);

formData.append('file', fileStream)

}

const jiraDomain = 'https://your-dealio.atlassian.net';
const url = `${jiraDomain}/rest/api/3/issue/${issueIdOrKey}/attachments`;
const token = `super secret token`;
const email = `some@email.com:`;

try {


const response = await axios.post(url, formData, {
headers: {
'Authorization': `Basic ${Buffer.from(`${email}:${token}`).toString('base64')}`,
'X-Atlassian-Token': 'nocheck',
'Content-Type': 'multipart/form-data; boundary=' + formData.getBoundary()
},
})
console.log(response?.data)

} catch (error) {
console.error('JIRA attachment error:', error);
}

}
1 vote
LostInMadness Forever December 2, 2019

any solution here ? 

1 vote
Ian Keller October 8, 2019

Abdul, did you ever get this figured out? I'm having the same problem. So far I have gotten 404, 415 and 500 errors. I can upload via Postman but when I try to use an Axios call it stops working.

0 votes
Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 20, 2017

I'm not sure about using axios, but from looking at other instances with this error such as

How to change the mediatype of an attachment via REST API

REST API error 415 unsupported media type

It seems like this might be a problem with a lack of a content type paramenter.   I think you might need to clarify the request by adding a

'Content-Type: application/json'

somehow to this REST.   But I also think you will need to modify the

'X-Atlassian-Token': 'nocheck'

to be

'X-Atlassian-Token: nocheck'

 

Suggest an answer

Log in or Sign up to answer