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

Conor Murphy December 15, 2017

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.

 

1 answer

0 votes
Kirubel Tesfaye
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 5, 2018

Don't you need to provide any sort of authentication?

Suggest an answer

Log in or Sign up to answer