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

Jeet April 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_Appfire
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.
April 11, 2023
Jeet April 11, 2023

Hi, @Naveen Kumar_Appfire, 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