Send HTTP-Post Request in EventListener

Hi, I am pretty new to developing Confluence plugins and some help would be very much appreciated.

Here is the task: I have created a space blueprint plugin - let's call it spaceplugin. I also use another plugin, that has a rest-api - let's call it restplugin. When a space is created through my blueprint I want my spaceplugin to send a http-post to the rest-api of the restplugin. I got it to work in the javascript file, in the submit function:

AJS.safe.ajax({ 
dataType: 'json',
type: 'POST',
url: "/rest/restplugin/1.0/create",
data: ({spaceKey : state.spaceKey, name: "Test"}),
});

 The problem is that the space doesn't exist here and it doesn't work. What I tried next was create an EventListener that listened on the SpaceBlueprintCreateEvent. This works also fine, but I can't authenticate myself because I always get the following response:

?xml version="1.0" encoding="UTF-8" standalone="yes"?><status><status-code>401</status-code><message>Client must be authenticated to access this resource.</message></status>

I have tried various methods of sending the post in my listener, non seemed to work. (HttpClient, RequestFactory, addBasicAuthentication, ...). Also I think that I can only use the com.atlassian.sal.api that is installed on my confluence server and things like addTrustedTokenAuthentication() are not recognized there.

My question is now the following:

Can anyone provide me a solution for my problem? It can either be updating the version of sal.api on confluence, using the right classes to send the post, using the right token or any solution on how to get the javascript to work. Code for the right implementation is also very much appreciated. Maybe I am just making a small mistake, but I can't get it to work. This is really a big deal for me and any help is very much appreciated. Even though my "current" code doesn't work, I am posting it here - maybe it helps understanding the problem.

String restUrl = settingsManager.getGlobalSettings().getBaseUrl() + "/rest/restplugin/1.0/create";
Request request = requestFactory.createRequest(Request.MethodType.POST, restUrl);
String jsonString = "{\"spaceKey\":" + event.getSpace().getKey() + ",\"name\":\"Test\"}";
request.setRequestContentType("application/json");
request.setRequestBody(jsonString);
request.execute();

 

1 answer

1 accepted

Update:

I got it to work by adding the basic authentication myself:

 request.addHeader("Authorization", "Basic XXXXXXXXXXXXXXXXX");

 XXXXXX represents the encoded username and password. If I add this username and password in the following, it doesn't work:

 request.addBasicAuthentication("http://localhost:8090/", "XXXX", "XXXXXX");

What am I doing wrong? Is the hostname wrong? Also, if I check for the headers there is simply no header added after calling that method.

Update2:

 

Got it to work with Basic and also Trusted Authentication. The problem was indeed the hostname, I just put "localhost" without the port and the protocol and it worked.

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 Jul 10, 2018 in Confluence

We want to see the templates you've created in Confluence!

Hi Community, Jessica here from the Confluence Product Marketing team!  July’s community challenge is all about sharing pictures  — and as an extension of our first post on what ...

713 views 21 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