'Add attachemet to a ticket using xhr2 for jira rest api

I am trying to upload a file to a ticket using Jira rest api and xhr2 using node.js with electron

Here is my code

var XMLHttpRequest = require('xhr2')
var xhr = new XMLHttpRequest();
const fs = require('fs');
const filePath = 'test2.txt';
const form = new FormData();
const stats = fs.statSync(filePath);
const fileSizeInBytes = stats.size;
const fileStream = fs.createReadStream(filePath);
form.append('file', fileStream, {knownLength: fileSizeInBytes});
xhr.open("POST", "https:/myInstance/rest/api/2/issue/ticketid/attachments", true);
xhr.setRequestHeader("Authorization" , "Basic "+ Buffer.from("user" + ":" + "token").toString("base64"));
xhr.setRequestHeader('Accept', 'application/json');
xhr.setRequestHeader('X-Atlassian-Token','no-check');
xhr.send(form);

I am getting the following error:

unsupported send() data [object FormData]

I am able to attach a file using: https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-attachments/#api-rest-api-3-issue-issueidorkey-attachments-post example

but I want to know what is the problem with my previous code

I tried to add these to the header:

xhr.setRequestHeader('processData', false);
xhr.setRequestHeader('contentType',false);

it didn't help, also I tried to add:

xhr.setRequestHeader("Content-type", "multipart/form-data");

also it didn't work



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source