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.
@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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
And yes, pasting the URL into a browser having my credentials in a cookie works as well.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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?
I have tried your code in browser console, and
Also, can you give me your use case where you want to access these API's in browser?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.