Upload file in confluence by AJAX

sergei.veit February 28, 2020

Hi,

I have the next code:

AP.context.getToken(function (token) {
var actionData = new FormData();
actionData.append('file', files[0]);
actionData.append('comment', "foobar");
actionData.append('minorEdit', "true");
actionData.append('atl_token', token);

AP.request({
type: 'PUT',
method: 'PUT',
headers: {
"X-Atlassian-Token": "nocheck",
'Accept': 'application/json'
},
data: actionData,
experimental: true,
cache: false,
contentType: 'multipart/form-data',
url: "/rest/api/content/" + PageId +
"/child/attachment",
success: function (data, status, response) {

console.log("SUCCESS", data, status, response);
},
error: function (response) {
console.log(response);
}
});

});

It is returning me 

{
"statusCode":400,
"data":{
"authorized":false,
"valid":true,
"errors":[],
"successful":false
},
"message":"com.atlassian.confluence.api.service.exceptions.BadRequestException: At least one attachment file must be included."
}

But I debug require module and found next - if content-type is "multipart/form-data", then they use data as object and convert it to formData.

Ok I send as object: 

AP.context.getToken(function (token) {
var actionData = {
comment: "check",
file: files[0],
minorEdit: true,
atl_token: token
};

AP.request({
type: 'PUT',
method: 'PUT',
headers: {
"X-Atlassian-Token": "nocheck",
'Accept': 'application/json'
},
data: actionData,
experimental: true,
cache: false,
contentType: 'multipart/form-data',
url: "/rest/api/content/" + PageId +
"/child/attachment",
success: function (data, status, response) {

console.log("SUCCESS", data, status, response);
},
error: function (response) {
console.log(response);
}
});

});

I have got: net::ERR_ACCESS_DENIED and 4 strange (2 before and 2 after): 

[Simple-XDM] Failed to validate origin: https://<>.atlassian.net

 

But in atlassian-connect.json

"scopes": [
"READ",
"WRITE",
"DELETE",
"SPACE_ADMIN",
"ADMIN",
"ACT_AS_USER"
],

I try to resolve that issue 2 days and I don't know how resolve it...

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events