Making API Calls In a Node.js Add-On Application

I'm making my first Node.js Add-On for Jira using the Atlassian-Connect Express framework and I'm having trouble making API calls.

In prior Add-Ons I used a basic JavaScript/PHP/Apache setup in order to create a connection between my own application and my JIRA instance using basic authorization (no JWT tokens). Making an API call was as simple as using AP.require.

I'm not entirely sure how to make API calls using ACE because when I use AP.require I receive a 403 error for simple GET requests. Should I be using Node to make API calls rather than AP.require? And where in ACE would I do this? I'm not sure if I have to create a route for making API calls or if I have to create a function in the Express build in app.js.

Originally I had my API calls setup in its own .js file as the following:

const baseUrl = "myinstance.atlassian.net";

function getAllProjects() {
return get(baseUrl + '/rest/api/2/project');
}

function get(url) {
return fetch(baseUrl + url).then(onSuccess, onError);
}

function onSuccess(response) {
return response.json();
}

function onError(error) {
console.log(error); // eslint-disable-line no-console
}

 When I try this I am unable to make a call to the API because it attaches my ngrok host link to the URL request like so:

ngrock_address/myinstance.atlassian.net/rest/api/2/project.

So I changed the code to the following:

const baseUrl = "myinstance.atlassian.net";
const token = document.getElementById("jwt-token").content;

function getAllProjects() {
return get(baseUrl + '/rest/api/2/project');
}

function get(url) {
AP.require('request', function(request) {
request({
url: url,
headers: {
'Authorization': `JWT ${token}`,
},
success: function(response) {
response = JSON.parse(response);
console.log(response);
},
error: function() {
console.log(arguments);
}
});
});
}

function onSuccess(response) {
return response.json();
}

function onError(error) {
console.log(error); // eslint-disable-line no-console
}

 But this only yields a 403 forbidden response.

 

0 answers

Suggest an answer

Log in or Join to answer
Community showcase
Teodora [Botron]
Published Feb 15, 2018 in Marketplace Apps

Jira Inferno: The Nine Circles of Jira Administration Hell

If you spend enough time as a Jira admin - whether you are managing a single, mid-sized instance, a large enterprise one or juggling multiple instances at once - you will eventually find yourself in ...

1,209 views 6 19
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot