0

I'm currently looking to use a rest API script to download attachments currently uploaded in JIRA. I have obtained the link URL in Postman but cannot figure out how to download to my PC.

I have tried the following commands but seem to get errors:

window.open(URL, _blank) - first attempt

https://stackoverflow.com/a/15832662 - second attempt

window.open(URL,_blank)

gives me the following error - ReferenceError: window is not defined

the code found in the stackoverflow link:

function downloadURI(uri, name) {
  var link = document.createElement("a");
  link.download = name;
  link.href = uri;
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
  delete link;
}

gives me the following error - ReferenceError: document is not defined

Here is my full code from the postman request:

let response = pm.response.json();

let test = response.fields.attachment;
attachmentnum = test.length;
let attachcontent=[];
let attachfilename=[];
console.log(attachmentnum);

for(let i=0; i!==(attachmentnum);i++){

    let push1 = response.fields.attachment[i];
    attachcontent.push(push1.content)
    attachfilename.push(push1.filename)

}
function downloadURI(uri, name) {
  var link = document.createElement("a");
  link.download = name;
  link.href = uri;
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
  delete link;
}

for(let i=0; i!==(attachmentnum);i++){
downloadURI(attachcontent [i], attachfilename[i])
}

pm.environment.set("attachcontent",JSON.stringify(attachcontent));
pm.environment.set("attachfilename",JSON.stringify(attachfilename));

Any help would be appreciated!

mbyx9jk2
  • 1
  • 1
  • Postman is designed for use as an HTTP debugging tool. You should probably use Node.js for this and not Postman. – Quentin Apr 05 '19 at 14:25
  • 1
    All your attempts assume you are running your code in a browser and not in Postman (or Node). – Quentin Apr 05 '19 at 14:27
  • You said you have an API to fetch the attachment from JIRA? Paste that code in your question. The code you currently have shown is client side javascript and should be ran in the browser, not on the server. That is why `document` is undefined. – Isaac Vidrine Apr 05 '19 at 14:38
  • I have added the full script to the comments as requested. This makes a lot of sense in why it works in a browser window but not postman (and is something I should have thought of! – mbyx9jk2 Apr 08 '19 at 08:22

1 Answers1

0

document and window objects are created by the browser at runtime. The reason they are undefined is because you are running this code on the server using Node. If your intention are to create a REST API to fetch and download the attatchment, you must define an http server object as well as an endpoint that will accomplish that task.

The code you have currently shown is simply creating a anchor element, setting the link href, and then calling the click() function on it. It is not a REST API, and is not something you can test in Postman, it is something that must be executed and tested in the browser.

Isaac Vidrine
  • 1,568
  • 1
  • 8
  • 20
  • This makes a lot of sense now that I consider it. The reason for running it via post man was to enable easy testing of the script and ideally some form of easy GUI for when someone else runs the script. – mbyx9jk2 Apr 08 '19 at 08:26