Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to send attachement uing Jira Rest API

Sumaia Kablan January 30, 2022

Hello

I am trying to send a file as attachment using Jira Rest API, I am doing this from inside the electron and I am doing the following:

I am setting the request (Post):

var XMLHttpRequest = require('xhr2');
var xhr = new XMLHttpRequest();
xhr.open("POST","myInstance/rest/api/2/issue/ticketid?attachements",true);
xhr.setRequestHeader("Authorization","Basic"+Buffer.from("myUserName"+":"+"myToken").toStrin("base64"));

I tried once
let file = "file=@'D:\\Testing Jira API\\testing.txt";
xhr.send(file);
xhr.onload = function (){ console.log("the statues is ", status);
and I am getting status 405 (method is not allowed)
also I tried :
let file = new File(["hello", new TextEncoder().encode("world.txt")],"hello.txt);
//using web-file-polyfill
and it didn't work
My question is what is the best approach to attach a file using Jira Rest API ?

4 answers

1 accepted

2 votes
Answer accepted
Pramodh M
Community Champion
January 30, 2022

Hi @Sumaia Kablan 

Welcome to the Community!!

Why Complexity? Just Uploading the file directly will do the work

Try this

curl --location \
--request POST 'https://your-domain.atlassian.net/rest/api/3/issue/TEST-123/attachments' \
-u 'email@example.com:<api_token>' \
-H 'X-Atlassian-Token: no-check' \
--form 'file=@"myfile.txt"'

https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-attachments/#api-rest-api-3-issue-issueidorkey-attachments-post

Thanks

Sumaia Kablan January 30, 2022

Thanx @Pramodh M @for your response .. but my task is requiring to do that using electron, or you mean by calling curl commands (for instance from the node.js itself in the electron using node-libcurl)?

Pramodh M
Community Champion
January 30, 2022

@Sumaia Kablan 

When Using Libraries, we should check once whether it's supported! ( XMLHttpRequest - not sure about this one here)

using Electron is fine, but may I know the method you are following here?

If you see the documentation, there are examples for various methods

0 votes
Sahil Sk March 10, 2025

What can’t I do 

0 votes
Sahil Sk March 10, 2025
Yar koi mujy b sikha da kasa krna ha ya
0 votes
Alex Medved _ConfiForms_
Community Champion
January 31, 2022

Your API endpoint is incorrect

Instead of 

http://myinstance/rest/api/2/issue/ticketid?attachements

you need to use

http://myinstance/rest/api/2/issue/ticketid/attachments

As per https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-attachments/#api-rest-api-3-issue-issueidorkey-attachments-post

Alex

Sumaia Kablan February 5, 2022

@Alex Medved _ConfiForms_ 

Okay, but I still have a problem if I am trying to use xmlHttpRequest using xhr2

var XMLHttpRequest = require('xhr2');
var xhr = new XMLHttpRequest();

const fs = require('fs);
const filePath = 'test2.txt';
const state = fs.stateSync(filePath);
const fileSizeInBytes = state.size;
const fileStream = fs.createReadStream(filePath);

form.append('file', fileStream, {knownLength: fileSizeInBytes});
xhr.opern("POST" , "httes://myinstance.atlassian.net/rest/api/2/issue/ticketid/attachements", true);
xhr.setRequestHeader("Authorization", ...);
xhr.setRequestHeader("Accept","application/json");
xhr.setRequestHeader("X-Atlassian-Token", "no-check");
xhr.send(form)

and I am getting unsupported send() data [object FormData]

 

and I think it is something regarding xhr2 more that jira API itself, so I followed their way

Alex Medved _ConfiForms_
Community Champion
February 5, 2022

You have a typo in 

httes://myinstance.atlassian.net/rest/api/2/issue/ticketid/attachements

And instead of "ticketid" you need to put an ACTUAL Jira issue key

As for "unsupported send() data [object FormData]" - cannot tell as I have no idea what "xhr2" is... if it's an npm package https://www.npmjs.com/package/xhr2 then probably the code makes sense

Anyway the error is on your side (in a script) - not on Jira

Sumaia Kablan February 5, 2022

@Alex Medved _ConfiForms_ 

for typos and actual jira issue key they are correct in my real code ..I just retyped it here instead of copying it

the problem is in xhr2 itself I guess

send() accepts the following data types: String, ArrayBufferView, ArrayBuffer (deprecated in the standard)

The following standard features are not implemented.

  • FormData
  • Blob
  • file:// URIs
  • data: URIs
  • upload progress events
  • synchronous operation
  • Same-origin policy checks and CORS
  • cookie processing

Thanx for your help

Sumaia Kablan February 5, 2022

@Alex Medved _ConfiForms_ 
Yes I know the problem was not in jira ..that is why I accepted the first answer the problem in my script is that I am using xhr2 with formData

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
TAGS
AUG Leaders

Atlassian Community Events