How to make a GET request on Bitbucket plugin rest api?

I have a bitbucket plugin that gives a REST API. I want to make a GET request on this API with a webapplication running on Angular4. First i got a lot of Prefligt errors. I have added the @CrossOrigin annotation in my API and now i receive 0 everytime i make a request on /project/number. If i return a simple number without accessing the Java API of bitbucket i receive the returned number, but if i access something from the Java API it return. So i am thinking that that it has something to do with the authentication. 

REST API:

@Path("/project")
@CrossOrigin
public class ProjectResource {

private ProjectMonitorService service;

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

@GET
@AnonymousAllowed
@Produces({MediaType.APPLICATION_JSON})
public Response getMessage() {
return Response.ok(new ProjectResourceModel("Hello World"+ service.getNumberOfProjects(), "")).build();
}

@GET
@AnonymousAllowed
@Produces({MediaType.APPLICATION_JSON})
@Path("number")
public Response getNumberOfProjects(){
service.login();
return Response.ok(new IntegerModel(service.getNumberOfProjects()))
.header("Access-Control-Allow-Headers", "*")
.build();
}

Angular Client: 


getProject(){
 
return this.http.get('http://localhost:7990/bitbucket/rest/project/1.0/project/number')
.map(res => res.json());
 
}
 
onClick(){
this.projectService.getProject().subscribe((number) => {
console.log(number)
});
 
What am i doing wrong? It returns status code 200 so everything should be okey, but I only get 0. Can someone explain how I need to do the authentication. 
Thanks
 

 

2 answers

This widget could not be displayed.

I never developed plugin for bit bucket but I think the behavior will be similar to confluence or jira. It is as you wrote, anonymous user does not have permission to view the projects so you will receive 0. You can try to use basic authentication and remove @AnonymousAllowed annotation just to check it is caused by request sent under anonymous user.

I tried it, it doesn't work. I used basic authentication and it gives me a 401 error. It is almost always a preflight error. So i tried to change some of the server settings and wrote a filter in the tomcat/conf/web.xml file and in the cargo-bitbucket-home/conf/web.xml and now I get a Options (url) 403 (forbidden). The filter that i wrote should allow everything. 

Fitler: 

<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.headers</param-name>
<param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,*</param-value>
</init-param>
<init-param>
<param-name>cors.exposed.headers</param-name>
<param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials,*</param-value>
</init-param>
<init-param>
<param-name>cors.support.credentials</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.preflight.maxage</param-name>
<param-value>10</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

If you need anything else just tell me

Thanks

If you ever did a plugin with a REST api and CORS was working there could i take a look?

This widget could not be displayed.

It will be as you wrote, anonymous user does not have permission to view the projects so 0 is returned by the service.

Try to use basic authentication with real user when creating request to check you're getting corrent number of projects.

sorry, I sent this accidentally...

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Aug 10, 2018 in Bitbucket

What is your #1 piece of advice to new users getting started using Bitbucket?

Hello Community! My name is Claire Maynard, and I’m a Product Marketing Manager on the Bitbucket team. I’m interested in hearing what advice or tips you have for new users getting started on ...

248 views 14 2
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