Hi there!
I’m working on a new app, but I’m struggling very hard with the fetch api.
I’m trying to upload a png file to a Confluence page using the PUT REST api endpoint.
So far, I’ve been hitting multiple roadblocks.
1. Problem with wrapped API call
When I wrote my first piece of code using node-fetch and ran it locally, I managed to get everything working with the form-data library for handling the multipart handling.
When I tried the same snippet in a Confluence Forge event trigger, I got errors stating that the file handle couldn’t be cloned (when trying to read from a file) or function() {} could not be cloned (when reading from an ArrayBuffer).
By creating the multipart body myself, I was able to get ahold of these cloning-related errors - but this introduces a lot of hassle in the code base.
2. Header Content-Type multipart is being rewritten as application/json
More problematic is the fact that I kept getting error 415 (unsupported media) while trying to upload the file. Using a plethora of console.log statements, I couldn’t find the cause. The same code running locally with node-fetch worked like a charm.
By using Pipedream, I was able to set up an endpoint and inspect the data being send over there in detail. It turned out that while I set the content-type to multipart, it ends up being application/json - causing the 415 unsupported media of course.
Since this is totally out of my reach to fix, I’m hoping to find a solution over here
Thanks a lot!
Tom
let base_url = '';
let pageId = '';
//...
let url = `${base_url}/wiki/rest/api/content/${pageId}/child/attachment?status=current`;
let upload_options = {
method: 'PUT',
headers: {
'X-Atlassian-Token': 'nocheck',
'Accept': 'application/json',
'Content-Type': `multipart/form-data; boundary=${boundary}`,
'Authorization': 'Basic CREDENTIALS'
},
body: body
}
fetch(url, upload_options).then((upload_result) => {
// No difference when using a product specific version of fetch
//api.asApp().requestConfluence(url, upload_options).then((upload_result) => {
console.log("Upload complete");
});
PS: I’m using the fetch with API token for now until the permission problem regarding the Forge user account created is resolved - just a temporary solution