Attach Image to confluence page

Hi all,

I’m trying to attach an image to the confluence page but I keep receiving a 403. I am following the steps here Confluence Docs. I’ve given permissions to all users and adding attachments is enabled. Here is my code:

var form = new FormData();
form.append("file",  $("#fileInput")[0].files[0]);

var data = {"file": form , "comment":"test attachment", "minorEdit": "true"};

AP.request({
          url: "https://my-test.atlassian.net/wiki/rest/api/content/76021794/child/attachment",
          type: "POST",
          data: data,
          contentType: "multipart/form-data",
          headers: {
              "X-Atlassian-Token": "nocheck",
              "Accept": "application/json"
          },
          success: function (data){
              return data;
          },
          error: function(jqXHR, textStatus, errorThrown){
              return deferred.reject(jqXHR.responseText);
          }
      });

Also trying on postman gives me a 403, current user not permitted to use Confluence. Any help will be appreciated!

Hi @JustinArias,

I have a few thoughts:

  1. Does your app have WRITE scope?
  2. I note you have given permissions to all users and adding attachments is enabled, but have you tried this with a curl command as shown in the docs since the 403 indicates a permission problem. AP.request acts with the permissions of the logged in user.
  3. I don’t know of the attachment size limitation, but have you tested this on small attachments? The reason I ask is that I saw the app properties API is incorrectly returning a 403 when attempting to PUT a property that is too large.

Regards,
Dugald

2 Likes

The issue was I didn’t have write as a scope in my atlassian-connect. Another issue was I was receiving a 500 for passing in the form instead of the direct file. Thank you so much.

I do have a side question. On server there was the ability to pass in allowDuplicate into the query params. Is this still available on cloud? If not what is the recommended way to handle duplicate filenames?

Hi @JustinArias,

That’s great that you are now able to add attachments. :slight_smile:

Regarding your second question about duplicate attachments, the create attachment resource doesn’t support overwriting attachments (400 error is returned), but the create or update attachment resource does.

If you want to avoid overwriting existing attachments, but guarantee that the attachment is added, then maybe consider a file naming scheme such as adding a timestamp or hash to prevent clashes.

Regards,
Dugald

2 Likes

Dear @JustinArias and @dmorrow,

Would you please help me at this? I have issue when use this code to attach a file:

var fileToUpload = document.getElementById(“testfile”).files[0];
var data = {“file”: fileToUpload , “comment”:“test attachment”, “minorEdit”: “true”};

AP.request({
url: “/rest/api/content/18415730/child/attachment”,
type: “POST”,
data: data,
contentType: “multipart/form-data”,
headers: {
“X-Atlassian-Token”: “nocheck”,
“Accept”: “application/json”
},
success: function (data){
return data;
},
error: function(jqXHR, textStatus, errorThrown){
//return deferred.reject(jqXHR.responseText);
}
});

An the error returns:

I can use curl command to attach successfully. Would you please let me know how to fix this issue?

Thanks!!!

Hi @elitesoftsp,

I’ve created a new topic for this.

Regards,
Dugald