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));
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));
}
This worked for me. Thank you!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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'
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.