It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

0 votes
Answer 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
Community showcase
Published in Confluence

6 Awesome Ways to Apply Trello, JIRA and Confluence to your Project

I attended  Atlassian Summit 2019  and learned a lot from the presenters, attendees and knowledgeable Atlassian product managers. The presentations I attended focused on applying Agile, pla...

3,843 views 15 38
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you