Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,361,397
Community Members
 
Community Events
168
Community Groups

Javascript API Call JIRA Cloud + Basic Auth does not work

P Mart I'm New Here Sep 08, 2022

 

Hello everyone,

I have a question reagrding how to call make a API call for JIRA Cloud API using Basic Authorization.
I want to get my Issues and then edit some values through REST API.

I follow some tutorials and examples

https://developer.atlassian.com/cloud/jira/platform/rest/v3/intro/

I took a look in
https://support.atlassian.com/user-management/docs/create-and-update-groups/

I think I must give premissions in the as I read here https://developer.atlassian.com/cloud/jira/platform/oauth-2-3lo-apps/

and for the basic authorization I shall get a api token here use the https://developer.atlassian.com/cloud/jira/platform/basic-auth-for-rest-apis/

I use this code:

```
<html>
<head>
</head>
<body>
<script>
console.log("called function httpGetRequest");


let url = 'https://my-domain.atlassian.net/rest/api/3/issue/TEST-2';
let user = '<my email address>';
let password = '<basic api token>';
let pass_base64 = btoa(password);

let auth_buff = user+":"+password;
let auth = btoa(auth_buff);


console.log ("auth : "+auth);


var xhr = new XMLHttpRequest();


xhr.open("GET", url);
xhr.withCredentials = true;


xhr.setRequestHeader('Access-Control-Allow-Origin', "+");
xhr.setRequestHeader("Authorization", "Basic " + auth);
xhr.setRequestHeader('Accept', 'application/json');


xhr.onreadystatechange = function () {
console.log( 'Response: '+ xhr.status + ' - '+xhr.responseText);
if (xhr.readyState === 4) {
console.log(xhr.status);
console.log(xhr.responseText);
}};

 

</script>
</body>
</html>
```


I get allways the error:


Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://my-domain.atlassian.net/rest/api/3/issue/TEST-2. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

XHR GET https://my-domain.atlassian.net/rest/api/3/issue/TEST-2.

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://my-domain.atlassian.net/rest/api/3/issue/TEST-2. . (Reason: CORS request did not succeed).

What can I do?

Best Regards,

Paul

1 answer

Hi @P Mart ,

The code you have works in a browser.  From a browser you can't call another domain, this is called a cross-origin request.

Your browser will block this request.

As an alternative, you could write your code in e.g. node, and run it from there.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Site Admin
TAGS

Atlassian Community Events