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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Sarah Goff-Dupont
Published 6 hours ago in Confluence

Think you know shares vs. @mentions in Confluence? Take this collab quiz.

To anyone who doubts that Atlassians are a little too obsessed with collaboration, and tools related thereto, let me describe a recent discussion we had (which took place on our internal Confluence, ...

71 views 2 3
Read article

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