Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Adding attachment in Jira Software through REST API always returns a 415

Simon Spalinger March 25, 2025

HI Community

 

In my forge application I am unable to add an attachment to an issue via Rest API.

Call within forge application returns 415 - unsupported media type

Call from standalone script works well


// call within my forge application alway returns in an 415 - unsupported media type

const
response = await api.asUser().requestJira(route`/rest/api/3/issue/${issueKey}/attachments`, {
method: 'POST',
body : form,
headers: {
'Accept': 'application/json',
'X-Atlassian-Token': 'no-check'
}
});



// call from a standlone scritp works

let response = await fetch('https://rwai-test.atlassian.net/rest/api/3/issue/TPD-178094/attachments', {
method: 'POST',
body : form,
headers: {
'Authorization': `Basic ${Buffer.from(EMAIL+':'+API_TOKEN).toString('base64')}`,
'Accept': 'application/json',
'X-Atlassian-Token': 'no-check'
}});

2 answers

1 accepted

0 votes
Answer accepted
Simon Spalinger April 2, 2025

finally found a way to make it work for me ... a little annoying that the examples from the official documentation do not work.

 

for me the following did work:

const formData = new FormData();
formData.append('file', Buffer.from(xml, 'utf-8'), { filename: filename });

const response = await api.asUser().requestJira(route`/rest/api/3/issue/${issueKey}/attachments`, {
method: 'POST',
body : formData,
headers: {
'Accept': 'application/json',
'X-Atlassian-Token': 'no-check',
'Content-Type': `multipart/form-data; boundary=${formData.getBoundary()}`,
}
});
0 votes
Vishal Biyani
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 25, 2025

@Simon Spalinger 

This forge sample code from Atlassian site is using /rest/api/2 instead of /rest/api/3

See if changing to version 2 makes a difference.

Otherwise, I don't see any other difference

// This sample uses Atlassian Forge and the `form-data` library. 
// https://developer.atlassian.com/platform/forge/
// https://www.npmjs.com/package/form-data
import api from "@forge/api";
import FormData from "form-data";
const form = new FormData();
form.append('file', fileStream, {knownLength: fileSizeInBytes});
const response = await api.asApp().requestJira('/rest/api/2/issue/{issueIdOrKey}/attachments',
{
method: 'POST',
body: form,
headers: { 'Accept': 'application/json', 'X-Atlassian-Token': 'no-check' } }
);
console.log(`Response: ${response.status} ${response.statusText}`);
console.log(await response.json());

 

Simon Spalinger March 25, 2025

Thanks for your hint and sorry, I should have mentioned it: I tried both version 2 and version 3 and see the same behaviour. It works from standalone script but not within the forge application.

Vishal Biyani
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 25, 2025

Can you try adding Content-type in the headers and see if this makes a difference?

I would guess multipart/form-data will be a better choice

Simon Spalinger April 2, 2025

@Vishal Biyani thanks for your hint - I tried the multipart/form-data but does not work either? Did you manage to upload an attachment within your forge application?

Vishal Biyani
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 2, 2025

Apologies I couldn't respond to your query in time. Great to see that you have it working

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events