Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Postman POST call works, but the same AJAX code does not work in JavaScript

I am using "/rest/api/content/22157686/child/attachment" call to upload an attachment to a Confluence space. When I try this in Postman, the file gets uploaded with no problem. I then tried to write JavaScript code to mimic that call. I used REST request. I could not get that to work even though several other of the Confluence API calls do work, including update a page. So I know the JavaScript is working and set up properly for the other API calls. For the create attachment call though, it seemed like the multipart/form-data was not being properly parsed and sent to the server. To simplify, I tried using AJAX instead of REST and copying the exact code from Postman. After some small modifications with headers, the call seems to be hitting the server, but the response is always null. Here is, more or less, the entire code. It is not long or complicated:

var FormData = require('form-data');
var form = new FormData();
form.append('file', 'C:/Users/<userid>/barnum-2.0/swagger.json');

settings = {
  'async': true,
  'crossDomain': true,
  'url': `https://share-staging.<company>.com/rest/api/content/22157686/child/attachment`,
  'method': 'POST',
  'dataType': 'json',
  'headers': {
  'X-Atlassian-Token': 'nocheck',
  'Cache-Control': 'no-cache',
  'Authorization': authorizationData,
  'X-Content-Type-Options': 'nosniff',
  'Accept': 'application/json; charset=utf-8'
  },
  'processData': false,
  'contentType': false,
  'mimeType': 'multipart/form-data',
  'multipart': true,
  'formData': form
}

   ajax(settings, function (response) {
      console.log('response :', response)
      done();
   });

1 answer

0 votes
Alex K Atlassian Team Jan 20, 2021

Hi David,

Thanks so much for your question.

It doesn't look like you are sending the file data, rather, you are sending a string of the file path. The correct way to send the file is by using a read stream (for example fs.createReadStream()). You can see this in the "form-data" npm library readme here: https://www.npmjs.com/package/form-data

Furthermore, if the server response is null then that could indicate an authentication issue. We also recommend monitoring the Confluence logs when sending the request to see if there are any obvious exceptions that indicate invalid request formatting. 

Thank you,
Alex K (Developer at Atlassian)

Confluence Data Center team

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence

🥓🙅🏻‍♀️ Meet-less May Badge!

Hello Confluence Community!  What if i told you that you could have a healthier life and be 100% meet-less? This month, we're promoting a healthy, balanced work diet with Confluence. We la...

135 views 2 12
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you