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'
}});
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()}`,
}
});
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());
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Apologies I couldn't respond to your query in time. Great to see that you have it working
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.