It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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
DPK J Community Leader Sep 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.

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.

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

DPK J Community Leader Sep 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?

Hi @DPK J, 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. 

DPK J Community Leader Oct 02, 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.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

274 views 1 2
Join discussion

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you