Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

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

Joel Suter September 5, 2017

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

0 votes
MoroSystems Support
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 5, 2017

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.

MoroSystems Support
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 5, 2017

sorry, I sent this accidentally...

0 votes
MoroSystems Support
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 5, 2017

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.

Joel Suter September 7, 2017

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

Joel Suter September 7, 2017

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events