Enable CORS in Bitbucket REST API

I am developing a plugin that provides a REST API. I want to retrieve some data from the api that i provided and display it on an web application on a different URL. So now i need to enable CORS. But i am running into a lot of problems. The code of my api looks like this:

@Path("/project")
@CrossOrigin("http://localhost:4200/")
public class ProjectResource {

private ProjectMonitorService service;

@Inject
ProjectResource(@ComponentImport ProjectMonitorService service) {
this.service = service;
}

@GET
@Produces({MediaType.APPLICATION_JSON})
public Response getMessage() {
return Response.ok(new ProjectResourceModel("Hell World", ""))
.header("Access-Control-Allow-Headers", "Authorization")
.header("Access-Control-Allow-Origin", "http://localhost:4200/")
.header("Access-Control-Allow-Methods", "OPTIONS, GET")
.header("Access-Control-Allow-Credentials", "*")
.build();
}

My client looks like this:

return this.http.get('http://localhost:7990/bitbucket/rest/project/1.0/project', { headers: new HttpHeaders().append('Authorization', 'Basic YWRtaW46YWRtaW4=') });  

Error:

OPTIONS http://localhost:7990/bitbucket/rest/project/1.0/project/number  XMLHttpRequest cannot load http://localhost:7990/bitbucket/rest/project/1.0/project/number. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 401  

Info: The authenticatin token is the one of the system admin   I tried putting a filter on the server that allows almost everything. The problem that i saw is that the Option method is not allowed and it is now possible to allow it(unsure). The problem is that the browser sends a OPTION preflight request. I have no clue how to enable the CORS, so can someone help me. 

1 answer

0 vote

Have you seen the below documentation page?

 

Also, not sure it can helps, but you can also have a look at: 

 

Finally, in case of no answers in here it may be a good idea to re-ask the same question into the developers' community:

 

I hope this helps.

 

 


Best Regards,
Dario

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Jun 12, 2018 in Bitbucket

Do you use any Atlassian products for your personal projects?

After spinning my wheels trying to get organized enough to write a book for National Novel Writing Month (NaNoWriMo) I took my affinity for Atlassian products from my work life and decided to tr...

24,010 views 26 12
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you