Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,551,823
Community Members
 
Community Events
184
Community Groups

Trying to add attachments in jira issue using rest api but getting [] in body from nodejs.

vishwajeet haldar
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
Apr 11, 2023

Here is the code for better understanding, 

 const filePath = 'file.png';

  const stats = fs.statSync(filePath);
  const fileSizeInBytes = stats.size;
  const fileStream = fs.createReadStream(filePath);

  const formData = new FormData();
  formData.append('file', fileStream, { knownLength: fileSizeInBytes });
  const formDataHeaders = formData.getHeaders();
  const response = await fetch(`${baseUrl}/ex/jira/${cloudId}/rest/api/2/issue/TEST-103/attachments`, {
    method: 'POST',
    body: formData,
    headers: {
      Authorization: accessToken,
      Accept: 'application/json',
      'X-Atlassian-Token': 'no-check',
      ...formDataHeaders,
    },
  });

1 answer

0 votes
Naveen Kumar
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
Apr 11, 2023
vishwajeet haldar
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
Apr 11, 2023 • edited

Hi, @Naveen Kumar, Thanks for your response, I followed the documentation, but I got an error: object is not iterable for FormData.

Well, I have found a solution that is working for me in Node.js without installing any additional library.

const formData = new FormData();    // no need to install for node 18.^ +.
const file = new Blob([fileBufferData]);  // creating a new Blob from fileBufferData
                                                                      // import { Blob } from 'node:buffer';
formData.append('file', file, filename);   // second parameter is Blob, third                                                                                        // parameter is filename. 
const url = `${baseUrl}/ex/jira/${cloudId}/rest/api/2/issue/${issueKey}/attachments`;
const options = {
           method: 'POST',
           headers: {
                 Accept: 'application/json',
                 'X-Atlassian-Token': 'nocheck',
                 Authorization: accessToken,   // `Bearer ${token}`
          },
    };
options.body = formData;
const response = await fetch(url, options);
return response.json();

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events