Why is my JS query to Jira Agile REST API failing while identical query to Jira REST API works?

Fredrik Åström September 28, 2019

I'm trying to fetch data from my Jira Cloud instance through the Jira and Jira Agile REST APIs using JavaScript in a browser. Queries to Jira REST API work fine but identical queries to Jira Agile REST API keep failing with the response

Response for preflight has invalid HTTP status code 401.

I'm using Basic Authentication with user ID and an API token obtained from Jira. With cURL and ARC, I'm able to successfully retrieve data both from the Jira REST API and the Jira Agile REST API, so the authentication against both APIs seems to work. In JS I have tried with both fetch() and jquery ajax() and the result was the same.

My code looks essentially as follows

function fetchFromJira(url, id, token) {
  const authorizationString = 'Basic ' + btoa(id + ':' + token);
  const options = {    method: 'GET',    headers: {
      Authorization: authorizationString,
      'Content-Type': 'application/json',
    },
  };
  fetch(url, options)
    .then(response => {
      if (response.ok) {
        return response.json();
      } else {
        throw new Error(response.status);
      }
    })
    .then(json => {      console.log(json);
    })
    .catch(error => {      console.log(error);
    });
}
fetchFromJira(
  'https://fredrikastrom.atlassian.net/rest/api/latest/issue/10000',
  '<user id>',
  '<API token>'
); // successful
fetchFromJira(
  'https://fredrikastrom.atlassian.net/rest/agile/1.0/board',
  '<user id>',
  '<API token>'
); // fails

Any clue what goes wrong in the query to Jira Agile REST API and how it should be modified in order to work? Thanks!

Same question previously posted to Stackoverflow.

1 answer

0 votes
DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 29, 2019

@Fredrik Åströmit seems like an authentication/authorization issue. Do you have permission to read boards?

Paste Board API URL in your browser where you are logged in as same user as you are using for above mentioned call. Check if you get any response there.

Fredrik Åström September 30, 2019

There does not seem to be any issue with the authentication or authorization per se, since I'm able to successfully connect and retrieve a list of boards with

curl -H "Authorization: Basic ########################" -X GET -H "Content-Type: application/json" https://fredrikastrom.atlassian.net/rest/agile/1.0/board

where the #'s stand for the string "<user id>:<api token>" base64 encoded.

Fredrik Åström September 30, 2019

And yes, pasting the URL into a browser having my credentials in a cookie works as well.

DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 30, 2019

@Fredrik Åströmthere seems to be no problem in your code here.

Have you validated that you are not getting CORS error in your browser?

  • You can look for that 'Network' tab of Developer Tools. If you your OPTIONS request succeed.

 

I have tried your code in browser console, and

  • It works fine for both requests if I am on console of same tab as of my site.
  • If I am on differnt tab or site,
    • Issues request for 'OPTIONS' return 200
    • Boards request for 'OPTIONS' return 401 (preflight error)

 

Also, can you give me your use case where you want to access these API's in browser?

Fredrik Åström October 1, 2019

Hi @DPKJ, yes I would get a CORS error in the browser as Atlassian is not supporting CORS in JIRA Cloud, but I'm circumventing this for the time being by using the "Allow CORS: Access-Control-Allow-Origin" extension in Chrome. So CORS is not the issue for now, and as said, calls to the "normal" Jira REST API (not Jira Agile) do work.

My use case is that I'm building an app for animating the flow of Jira issues through the workflow over the course of a project. 

DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 2, 2019

@Fredrik Åström I got your point. Let me try out this with extension and see if this works on my site.

One quick side note, if you are building an addon, you can use JavaScript API provided by Atlassian to fetch data.

Here AP.request is recommended to call rest api from Jira site.

DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 2, 2019

Suggest an answer

Log in or Sign up to answer